From 154a0a114efd5f4d84c34d7678c037f399dd0d4c Mon Sep 17 00:00:00 2001 From: Aftabbs Date: Mon, 28 Sep 2026 07:08:40 +0530 Subject: [PATCH] fix: guard double-decode to only unwrap when result is a dict Addresses Greptile P1: valid JSON string arguments like "123" were being coerced to integers because json.loads() was applied on the string result unconditionally. Only unwrap when the second decode produces a dict, which is the only valid shape for tool call arguments. Also removes disallowed explanatory comments per litellm AGENTS.md. --- .../prompt_templates/common_utils.py | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/litellm/litellm_core_utils/prompt_templates/common_utils.py b/litellm/litellm_core_utils/prompt_templates/common_utils.py index cdc8da07321..9838efa5db8 100644 --- a/litellm/litellm_core_utils/prompt_templates/common_utils.py +++ b/litellm/litellm_core_utils/prompt_templates/common_utils.py @@ -2384,19 +2384,15 @@ def parse_tool_call_arguments( try: 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 "", - context or "unknown context", - ) try: - parsed = json.loads(parsed) + _second_decode = json.loads(parsed) + if isinstance(_second_decode, dict): + verbose_logger.warning( + "Tool call arguments for tool '%s' (%s) were double-encoded", + tool_name or "", + context or "unknown context", + ) + parsed = _second_decode except json.JSONDecodeError: pass return parsed