Python bindings for lambda-Tool: safe LLM tool composition
Project description
lambda-tool-python
Python bindings for λ-Tool: safe LLM tool composition with type checking.
λ-Tool is a typed intermediate representation that LLMs generate instead of Python for tool composition. The type checker verifies code is safe before execution -- no missing fields, no unhandled errors, no infinite loops.
Prerequisites
- Python 3.10+
- The
lambda_toolCLI binary (build from source)
# Build the OCaml CLI
cd lambda-tool
opam install . --deps-only && eval $(opam env)
dune build && dune install
Install
pip install lambda-tool
The package auto-discovers the lambda_tool binary from PATH, dune build output, or opam install locations.
Quick Start
from lambda_tool import LambdaTool, LambdaToolError
lt = LambdaTool()
# Type check a program
result = lt.typecheck("let x = 1 + 2 in x")
print(result.type) # "Int"
print(result.effects) # []
# Execute with real tool implementations
result = lt.run("""
tool query: String -{Read}-> {rows: List {id: Int, name: String}};
let get_name = fn row: {id: Int, name: String} => row.name in
match exec tool query "SELECT * FROM users" {
Ok(result) => map get_name result.rows,
Err(e) => []: String
}
""", executors={
"query": lambda arg: {"rows": [{"id": 1, "name": "Alice"}]},
})
print(result.value) # ["Alice"]
print(result.type) # "List String"
print(result.effects) # ["Read"]
Error Handling
Type errors are caught before execution:
try:
lt.typecheck("let x = 1 + true in x")
except LambdaToolError as e:
print(e.errors) # ["Type mismatch at line 1, col 9: expected Bool, got Int"]
Runtime tool errors are caught and converted to Err values:
def flaky_tool(arg):
raise ConnectionError("network down")
result = lt.run("""
tool fetch: String -{Network}-> String;
match exec tool fetch "url" {
Ok(data) => data,
Err(e) => "fallback"
}
""", executors={"fetch": flaky_tool})
print(result.value) # "fallback"
API Reference
LambdaTool(binary=None)
Create a wrapper instance. Pass binary to override auto-discovery of the lambda_tool CLI.
lt.typecheck(source) -> TypeCheckResult
Type check without executing. Returns TypeCheckResult with:
.type(str) -- the inferred type.effects(list[str]) -- the required effects ("Read","Write","Network")
Raises LambdaToolError on parse or type errors.
lt.run(source, executors=None) -> RunResult
Type check and execute. Returns RunResult with:
.value-- the program's return value (decoded to Python).type(str) -- the inferred type.effects(list[str]) -- the required effects
Raises LambdaToolError on parse, type, or runtime errors.
Executor Format
Each executor is a Python callable (arg) -> result:
| Description | |
|---|---|
| Input | Decoded tool argument: dict for records, str/int/bool for primitives, list for lists |
| Output | Any JSON-serializable value (dict, list, str, int, bool, None) |
| Errors | Raise any exception to signal a tool error (caught and converted to Err) |
Value Decoding
| λ-Tool | Python |
|---|---|
42, true, "hello" |
42, True, "hello" |
{name = "Alice"} |
{"name": "Alice"} |
[1, 2, 3] |
[1, 2, 3] |
Ok(v) |
("Ok", v) |
Err(e) |
("Err", e) |
() |
None |
Testing
pytest tests/ -v
17 tests covering type checking, execution, interactive tool callbacks, traverse, conditionals, and edge cases.
Related Projects
- lambda-tool: The core OCaml implementation (type checker, interpreter, CLI)
- minilambda: Minimal agent demo using Claude + λ-Tool
License
LGPL-3.0-or-later. Check LICENSE for details. © Sarthak Shah (matchcase), 2026.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lambda_tool_python-0.1.0.tar.gz.
File metadata
- Download URL: lambda_tool_python-0.1.0.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
36c9b5c6e5f4a3aa518f6ab30ef97d7fc9c55e32d8edb5fae6d8060e0f1a4bd3
|
|
| MD5 |
48a008f0359de2a3d74e563b8ff4c17c
|
|
| BLAKE2b-256 |
e59862bc78cf6a2f072105e7bdd3d43f0590b3ebce11356869766142ca0c10c0
|
File details
Details for the file lambda_tool_python-0.1.0-py3-none-any.whl.
File metadata
- Download URL: lambda_tool_python-0.1.0-py3-none-any.whl
- Upload date:
- Size: 21.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40c701e35057adfb7a3a628fcb9dd85a39518f36c43bd1afd0196c3c18d842df
|
|
| MD5 |
131be3aebb83759a8e542da99ccdb554
|
|
| BLAKE2b-256 |
b017f3573315b79aad95189deb2897d03058405acce7f403d2c1622055987297
|