Skip to content

Conversation

@m7mdhka
Copy link

@m7mdhka m7mdhka commented Jan 26, 2026

Summary

Fixes a critical bug where exceptions raised during tool execution in LiteLLMProvider.complete_with_tools() would crash the entire LLM generation loop, causing loss of conversation context and preventing graceful error recovery.

Problem

The complete_with_tools() method only caught JSONDecodeError when parsing tool arguments, but did not handle exceptions raised during actual tool execution. When a tool raised an exception:

  • ❌ The entire LLM generation process crashed
  • ❌ Conversation context was lost
  • ❌ The agent could not recover or retry
  • ⚠️ Security concern: Uncaught exceptions could leak sensitive stack trace information

Solution

  • Wrapped tool_executor(tool_use) calls in comprehensive try/except blocks
  • Convert tool exceptions into structured ToolResult errors with error details
  • Normalize unexpected return types (dict/str) from custom executors into ToolResult
  • Added error logging for tool failures
  • Tool failures are now communicated back to the LLM, allowing it to:
    • Retry with different parameters
    • Use alternative tools
    • Inform the user about the failure

Changes

  • core/framework/llm/litellm.py: Added exception handling and result normalization in complete_with_tools()
  • core/tests/test_litellm_provider.py: Added regression test test_complete_with_tools_tool_exception_is_captured
  • CHANGELOG.md: Documented the fix

Testing

All 23 tests pass, including the new regression test that verifies:

  • Tool exceptions are caught (no crash)
  • Errors are converted to ToolResult with is_error=True
  • The LLM loop continues and receives the error message
  • Conversation context is preserved

uv run pytest tests/test_litellm_provider.py -v

23 passed

Wrap tool_executor calls in try/except to prevent crashes when tools
raise exceptions. Tool failures are now converted to structured ToolResult
errors, allowing the LLM to gracefully handle failures and retry or use
alternative tools.

- Added exception handling around tool_executor(tool_use) calls
- Normalize unexpected return types to ToolResult
- Added error logging for tool failures
- Added regression test to verify exception handling
- Updated CHANGELOG.md
@m7mdhka
Copy link
Author

m7mdhka commented Jan 26, 2026

#801

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants