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.
Release files for lambda-tool-python 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| lambda_tool_python-0.1.0.tar.gz | 22.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lambda_tool_python-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 43.9 kB
Release files / lambda_tool_python-0.1.0.tar.gz
| Download URL | lambda_tool_python-0.1.0.tar.gz |
|---|---|
| Size | 22.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
36c9b5c6e5f4a3aa518f6ab30ef97d7fc9c55e32d8edb5fae6d8060e0f1a4bd3
|
|
BLAKE2b-256 checksum How to use checksums |
e59862bc78cf6a2f072105e7bdd3d43f0590b3ebce11356869766142ca0c10c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Release files / lambda_tool_python-0.1.0-py3-none-any.whl
| Download URL | lambda_tool_python-0.1.0-py3-none-any.whl |
|---|---|
| Size | 21.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
40c701e35057adfb7a3a628fcb9dd85a39518f36c43bd1afd0196c3c18d842df
|
|
BLAKE2b-256 checksum How to use checksums |
b017f3573315b79aad95189deb2897d03058405acce7f403d2c1622055987297
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|