mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-09 03:18:44 +00:00
fix(anthropic): unwrap double-encoded tool_call arguments for Anthropic providers
When agent frameworks store/load tool call history they sometimes apply json.dumps twice to function.arguments, producing a JSON string whose first decode still returns a str. Anthropic tool_use.input must be an object; the extra string layer causes Azure AI Anthropic to return: messages.x.content.y.tool_use.input: Input should be an object parse_tool_call_arguments now detects this case: when json.loads() returns a str it attempts one additional decode and logs a warning naming the tool so callers can trace the source of the double-encoding. Fixes #42739 Signed-off-by: Aftabbs <aftabbs.wwe@gmail.com>
This commit is contained in:
parent
b1194aa373
commit
923b5f9b41
1 changed files with 19 additions and 2 deletions
|
|
@ -1,4 +1,4 @@
|
|||
"""
|
||||
"""
|
||||
Common utility functions used for translating messages across providers
|
||||
"""
|
||||
|
||||
|
|
@ -2382,7 +2382,24 @@ def parse_tool_call_arguments(
|
|||
return {}
|
||||
|
||||
try:
|
||||
return json.loads(arguments)
|
||||
parsed = json.loads(arguments)
|
||||
if isinstance(parsed, str):
|
||||
# Double-encoded JSON: json.dumps applied twice by the caller (common
|
||||
# in agent frameworks that serialise tool arguments through a store).
|
||||
# Anthropic's tool_use.input must be an object, not a string, so
|
||||
# unwrap one extra layer and warn so callers can fix the root cause.
|
||||
verbose_logger.warning(
|
||||
"Tool call arguments for tool '%s' (%s) decoded to a string "
|
||||
"instead of an object — double-encoded JSON detected. "
|
||||
"Attempting second decode.",
|
||||
tool_name or "<unknown>",
|
||||
context or "unknown context",
|
||||
)
|
||||
try:
|
||||
parsed = json.loads(parsed)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
return parsed
|
||||
except json.JSONDecodeError as original_error:
|
||||
repaired: Final = _attempt_json_repair(arguments)
|
||||
if repaired is not None:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue