Skip to content

Conversation

@riunyfir
Copy link
Contributor

Description:
This PR fixes a KeyError: 'tools' that occurs in LLMToolSelectorMiddleware when the LLM returns a response missing the expected 'tools' key. The middleware now gracefully handles malformed or incomplete responses by falling back to using all available tools (respecting the max_tools limit) instead of raising an exception.
Problem:

When using LLMToolSelectorMiddleware with an agent, the middleware calls an LLM to select relevant tools before the main model invocation. However, if the LLM response doesn't strictly follow the structured output schema (e.g., missing the 'tools' key or having an invalid type), the code would raise a KeyError at line 229 in tool_selection.py, interrupting all downstream processing.

This issue occurs intermittently, especially with complex prompts or edge cases where the LLM doesn't strictly adhere to the expected schema format.

Solution:
The fix adds defensive checks in _process_selection_response() to handle three scenarios:

  1. Missing 'tools' key: When the response dictionary doesn't contain a 'tools' key, the middleware logs a warning and falls back to using all available tools (up to max_tools limit).

  2. Invalid 'tools' type: When the 'tools' key exists but is not a list, the middleware logs a warning and falls back to using all available tools.

  3. Normal case: When the response is valid, the existing logic continues to work as before.

This approach ensures backward compatibility while making the middleware more resilient to LLM response variations. The fallback behavior (using all available tools) is reasonable because:

  • It maintains functionality even when tool selection fails
  • The max_tools limit is still respected
  • always_include tools are still added as expected
  • Users get a warning in logs to help debug LLM response issues

Changes

@github-actions github-actions bot added the langchain `langchain` package issues & PRs label Dec 15, 2025
@riunyfir riunyfir changed the title fix: handle missing 'tools' key in LLMToolSelectorMiddleware response fix: handle missing 'tools' key in LLMToolSelectorMiddleware response Dec 15, 2025
@github-actions github-actions bot added the fix For PRs that implement a fix label Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix For PRs that implement a fix langchain `langchain` package issues & PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 KeyError: 'tools' in LLMToolSelectorMiddleware when model response misses expected key

1 participant