Tiny LangChain middleware for better tool-call recovery, loop detection, and structured failure messages.
Project description
langchain-tool-recover
A tiny LangChain middleware that stops agents from looping, failing silently, or retrying tools stupidly.
langchain-tool-recover is not another retry library. LangChain already has ToolRetryMiddleware, ToolCallLimitMiddleware, and create_agent middleware. This package fills the smaller wedge around tool-call recovery UX: duplicate-call loop detection, structured failure messages, empty-result recovery, and sane LangChain tool defaults.
Install
pip install langchain-tool-recover
For local development:
pip install -e ".[dev]"
Use With create_agent
from langchain.agents import create_agent
from langchain_tool_recover import ToolRecoverMiddleware
agent = create_agent(
model=model,
tools=tools,
middleware=[
ToolRecoverMiddleware(),
],
)
When a tool fails or returns an empty result, the agent receives compact JSON it can reason over:
{"status":"needs_replan","tool":"search_docs","failure_class":"empty_result","message":"The tool returned no results.","suggestion":"Try broadening the query or removing exact-match terms.","action":"return_to_agent","attempt":1}
Use As A Tool Wrapper
from langchain_tool_recover import recover_tool
safe_search = recover_tool(search_tool)
The wrapper preserves the tool name, description, argument schema, and async support where LangChain exposes it.
Before And After
Without this library:
Agent calls search_docs("foo")
No results.
Agent calls search_docs("foo")
No results.
Agent calls search_docs("foo")
No results.
With this library:
Agent calls search_docs("foo")
No results.
[tool-recover] search_docs attempt=1 status=empty_result
[tool-recover] suggestion=Try broadening the query or removing exact-match terms.
[tool-recover] action=returned_recovery_message
Suggestion sent to agent: broaden the query or try another tool.
Duplicate calls are tracked per run. The first duplicate logs a warning and executes. The second duplicate returns a needs_replan message. The third and later duplicates are blocked for that run.
Default Policy
| Failure class | Default action |
|---|---|
timeout |
retry_with_backoff |
rate_limit |
retry_with_backoff |
validation_error |
return_to_agent |
auth_error |
fail_fast |
empty_result |
return_to_agent |
duplicate_call |
block after duplicate limit |
unsafe_command |
block |
unknown_error |
fail_fast |
Override policies by passing a mapping:
from langchain_tool_recover import FailureClass, RecoveryAction, ToolRecoverMiddleware
middleware = ToolRecoverMiddleware(
policies={
FailureClass.UNKNOWN_ERROR: RecoveryAction.RETURN_TO_AGENT,
}
)
What It Classifies
The classifier is deterministic and conservative:
- Exceptions: timeout, rate limit, validation error, auth error, unsafe command, unknown error.
- Empty results:
None, blank strings, empty collections, and explicit empty sentinels such as{"empty": true}or{"results": []}. - LangChain
ToolMessage(status="error")content is reclassified when the tool node converts invocation errors into messages.
It intentionally does not try to infer complex semantic failure from arbitrary prose in v1.
Examples
examples/basic_agent.pyexamples/duplicate_tool_loop.pyexamples/empty_search_recovery.py
Development
pip install -e ".[dev]"
pytest
python -m build
ruff check .
License
MIT
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 langchain_tool_recover-0.1.0.tar.gz.
File metadata
- Download URL: langchain_tool_recover-0.1.0.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c98789473a8b2c65a08766d093bdda37dacaa8612d92a7b749bc3cf6c9323de0
|
|
| MD5 |
7dc296f821c7f4b1a71a33479e542917
|
|
| BLAKE2b-256 |
28f053289d5db0a367b58d3af960384c55654105eb4bf2cf9ae67c3e03a98aef
|
Provenance
The following attestation bundles were made for langchain_tool_recover-0.1.0.tar.gz:
Publisher:
publish.yml on Trihedron1240/langchain-tool-recover
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_tool_recover-0.1.0.tar.gz -
Subject digest:
c98789473a8b2c65a08766d093bdda37dacaa8612d92a7b749bc3cf6c9323de0 - Sigstore transparency entry: 1380808948
- Sigstore integration time:
-
Permalink:
Trihedron1240/langchain-tool-recover@a2593f151b7f7062a6b9e89de11b11caf4f65ec6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Trihedron1240
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a2593f151b7f7062a6b9e89de11b11caf4f65ec6 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file langchain_tool_recover-0.1.0-py3-none-any.whl.
File metadata
- Download URL: langchain_tool_recover-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1809bf80bcd9ceb9964a6f42ecbfd3cded93f655a38f73f97719b789f0c9a9d3
|
|
| MD5 |
e95df6dbbcb2a9a6d045607644a03b4f
|
|
| BLAKE2b-256 |
8d7a664afcf5bd229e4a151843348e2277e42e92b840b4dde28457e59d10fcef
|
Provenance
The following attestation bundles were made for langchain_tool_recover-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on Trihedron1240/langchain-tool-recover
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_tool_recover-0.1.0-py3-none-any.whl -
Subject digest:
1809bf80bcd9ceb9964a6f42ecbfd3cded93f655a38f73f97719b789f0c9a9d3 - Sigstore transparency entry: 1380809098
- Sigstore integration time:
-
Permalink:
Trihedron1240/langchain-tool-recover@a2593f151b7f7062a6b9e89de11b11caf4f65ec6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Trihedron1240
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a2593f151b7f7062a6b9e89de11b11caf4f65ec6 -
Trigger Event:
workflow_dispatch
-
Statement type: