Compare commits

..

4 Commits

Author SHA1 Message Date
5c73f30583 fix: patch parse_tool_call bug that returns string args instead of dict
Root cause identified: langchain_core's parse_tool_call() sometimes returns
tool_calls with 'args' as a JSON string instead of parsed dict object.

This violates AIMessage's Pydantic schema which expects args to be dict.

Solution: Wrapper now detects when parse_tool_call returns string args
and immediately converts them to dict using json.loads().

This is a workaround for what appears to be a LangChain bug where
parse_tool_call's json.loads() call either:
1. Fails silently without raising exception, or
2. Succeeds but result is not being assigned to args field

The fix ensures AIMessage always receives properly parsed dict args,
resolving Pydantic validation errors for all DeepSeek tool calls.
2025-11-06 17:58:41 -05:00
b73d88ca8f fix: normalize DeepSeek non-standard tool_calls format
Systematic debugging revealed DeepSeek returns tool_calls in non-standard
format that bypasses LangChain's parse_tool_call():

**Root Cause:**
- OpenAI standard: {function: {name, arguments}, id}
- DeepSeek format: {name, args, id}
- LangChain's parse_tool_call() returns None when no 'function' key
- Result: Raw tool_call with string args → Pydantic validation error

**Solution:**
- ToolCallArgsParsingWrapper detects non-standard format
- Normalizes to OpenAI standard before LangChain processing
- Converts {name, args, id} → {function: {name, arguments}, id}
- Added diagnostic logging to identify format variations

**Impact:**
- DeepSeek models now work via OpenRouter
- No breaking changes to other providers (defensive design)
- Diagnostic logs help debug future format issues

Fixes validation errors:
  tool_calls.0.args: Input should be a valid dictionary
  [type=dict_type, input_value='{"symbol": "GILD", ...}', input_type=str]
2025-11-06 17:51:33 -05:00
d199b093c1 debug: patch parse_tool_call to identify source of string args
Added global monkey-patch of langchain_core's parse_tool_call to log
the type of 'args' it returns. This will definitively show whether:
1. parse_tool_call is returning string args (bug in langchain_core)
2. Something else is modifying the result after parse_tool_call returns
3. AIMessage construction is getting tool_calls from a different source

This is the critical diagnostic to find the root cause.
2025-11-06 17:42:33 -05:00
483621f9b7 debug: add comprehensive diagnostics to trace error location
Adding detailed logging to:
1. Show call stack when _create_chat_result is called
2. Verify our wrapper is being executed
3. Check result after _convert_dict_to_message processes tool_calls
4. Identify exact point where string args become the problem

This will help determine if error occurs during response processing
or if there's a separate code path bypassing our wrapper.
2025-11-06 12:10:29 -05:00

View File

@@ -32,14 +32,45 @@ class ToolCallArgsParsingWrapper:
# Model doesn't have this method (e.g., MockChatModel), skip patching
return
# CRITICAL: Patch parse_tool_call in base.py's namespace (not in openai_tools module!)
from langchain_openai.chat_models import base as langchain_base
original_parse_tool_call = langchain_base.parse_tool_call
def patched_parse_tool_call(raw_tool_call, *, partial=False, strict=False, return_id=True):
"""Patched parse_tool_call to fix string args bug and add logging"""
result = original_parse_tool_call(raw_tool_call, partial=partial, strict=strict, return_id=return_id)
if result:
args_type = type(result.get('args', None)).__name__
print(f"[DIAGNOSTIC] parse_tool_call returned: args type = {args_type}")
if args_type == 'str':
print(f"[DIAGNOSTIC] ⚠️ BUG FOUND! parse_tool_call returned STRING args, fixing...")
# FIX: parse_tool_call sometimes returns string args instead of dict
# This happens when it fails to parse but doesn't raise an exception
try:
result['args'] = json.loads(result['args'])
print(f"[DIAGNOSTIC] ✓ Fixed! Converted string args to dict")
except (json.JSONDecodeError, TypeError) as e:
print(f"[DIAGNOSTIC] ❌ Failed to parse args: {e}")
# Leave as string if we can't parse it
return result
# Replace in base.py's namespace (where _convert_dict_to_message uses it)
langchain_base.parse_tool_call = patched_parse_tool_call
original_create_chat_result = self.wrapped_model._create_chat_result
@wraps(original_create_chat_result)
def patched_create_chat_result(response: Any, generation_info: Optional[Dict] = None):
"""Patched version with diagnostic logging and args parsing"""
import traceback
response_dict = response if isinstance(response, dict) else response.model_dump()
# DIAGNOSTIC: Log response structure for debugging
print(f"\n[DIAGNOSTIC] _create_chat_result called")
print(f" Response type: {type(response)}")
print(f" Call stack:")
for line in traceback.format_stack()[-5:-1]: # Show last 4 stack frames
print(f" {line.strip()}")
print(f"\n[DIAGNOSTIC] Response structure:")
print(f" Response keys: {list(response_dict.keys())}")
@@ -116,7 +147,18 @@ class ToolCallArgsParsingWrapper:
# Keep as-is if serialization fails
# Call original method with fixed response
return original_create_chat_result(response_dict, generation_info)
print(f"[DIAGNOSTIC] Calling original_create_chat_result...")
result = original_create_chat_result(response_dict, generation_info)
print(f"[DIAGNOSTIC] original_create_chat_result returned successfully")
print(f"[DIAGNOSTIC] Result type: {type(result)}")
if hasattr(result, 'generations') and result.generations:
gen = result.generations[0]
if hasattr(gen, 'message') and hasattr(gen.message, 'tool_calls'):
print(f"[DIAGNOSTIC] Result has {len(gen.message.tool_calls)} tool_calls")
if gen.message.tool_calls:
tc = gen.message.tool_calls[0]
print(f"[DIAGNOSTIC] tool_calls[0]['args'] type in result: {type(tc['args'])}")
return result
# Replace the method
self.wrapped_model._create_chat_result = patched_create_chat_result