Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/google/adk/models/lite_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,21 @@ def _content_to_message_param(
tool_messages = []
for part in content.parts:
if part.function_response:
# FIX: Check if response is already a string before serializing.
# MCP tool responses come as JSON strings, but _safe_json_serialize was
# double-serializing them (json.dumps on already-JSON strings), causing
# triple-nested JSON like: '{"content": [{"type": "text", "text": "{\n \"type\"..."}]}'
# This prevented Claude/GPT from parsing tool results correctly.
response_content = (
part.function_response.response
if isinstance(part.function_response.response, str)
else _safe_json_serialize(part.function_response.response)
)
tool_messages.append(
ChatCompletionToolMessage(
role="tool",
tool_call_id=part.function_response.id,
content=_safe_json_serialize(part.function_response.response),
content=response_content,
)
)
Comment on lines +370 to 386
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the logic is correct, this implementation can be made more concise. The temporary response_content variable can be inlined directly into the ChatCompletionToolMessage constructor. Additionally, the detailed comment is great for a pull request description but could be summarized for better long-term readability within the code.

      # MCP tool responses can be pre-serialized JSON strings. Avoid
      # double-serializing them to prevent parsing issues by downstream models.
      tool_messages.append(
          ChatCompletionToolMessage(
              role="tool",
              tool_call_id=part.function_response.id,
              content=(
                  part.function_response.response
                  if isinstance(part.function_response.response, str)
                  else _safe_json_serialize(part.function_response.response)
              ),
          )
      )

if tool_messages:
Expand Down