mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-12 05:27:24 -04:00
fix: resolve DeepSeek tool_calls validation errors (production ready)
After extensive systematic debugging, identified and fixed LangChain bug where parse_tool_call() returns string args instead of dict. **Root Cause:** LangChain's parse_tool_call() has intermittent bug returning unparsed JSON string for 'args' field instead of dict object, violating AIMessage Pydantic schema. **Solution:** ToolCallArgsParsingWrapper provides two-layer fix: 1. Patches parse_tool_call() to detect string args and parse to dict 2. Normalizes non-standard tool_call formats to OpenAI standard **Implementation:** - Patches parse_tool_call in langchain_openai.chat_models.base namespace - Defensive approach: only acts when string args detected - Handles edge cases: invalid JSON, non-standard formats, invalid_tool_calls - Minimal performance impact: lightweight type checks - Thread-safe: patches apply at wrapper initialization **Testing:** - Confirmed fix working in production with DeepSeek Chat v3.1 - All tool calls now process successfully without validation errors - No impact on other AI providers (OpenAI, Anthropic, etc.) **Impact:** - Enables DeepSeek models via OpenRouter - Maintains backward compatibility - Future-proof against similar issues from other providers Closes systematic debugging investigation that spanned 6 alpha releases. Fixes: tool_calls.0.args validation error [type=dict_type, input_type=str]
This commit is contained in:
10
CHANGELOG.md
10
CHANGELOG.md
@@ -9,10 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Fixed
|
||||
- Fixed Pydantic validation errors when using DeepSeek models via OpenRouter
|
||||
- Root cause: DeepSeek returns tool_calls in non-standard format with `args` field directly, bypassing LangChain's `parse_tool_call()`
|
||||
- Solution: Added `ToolCallArgsParsingWrapper` that normalizes non-standard tool_call format to OpenAI standard before LangChain processing
|
||||
- Wrapper converts `{name, args, id}` → `{function: {name, arguments}, id}` format
|
||||
- Includes diagnostic logging to identify format inconsistencies across providers
|
||||
- Root cause: LangChain's `parse_tool_call()` has a bug where it sometimes returns `args` as JSON string instead of parsed dict object
|
||||
- Solution: Added `ToolCallArgsParsingWrapper` that:
|
||||
1. Patches `parse_tool_call()` to detect and fix string args by parsing them to dict
|
||||
2. Normalizes non-standard tool_call formats (e.g., `{name, args, id}` → `{function: {name, arguments}, id}`)
|
||||
- The wrapper is defensive and only acts when needed, ensuring compatibility with all AI providers
|
||||
- Fixes validation error: `tool_calls.0.args: Input should be a valid dictionary [type=dict_type, input_value='...', input_type=str]`
|
||||
|
||||
## [0.4.1] - 2025-11-06
|
||||
|
||||
|
||||
Reference in New Issue
Block a user