Skip to content

Crashes in trace_call_llm() on non-serializable data #878

Description

@stephensmithnewfront

Describe the bug
Calls to trace_call_llm() happen in agent flow, but trace_call_llm() crashes when it tries to serialize bytes data.

2025-05-22 19:47:11,764 - ERROR - fast_api.py:817 - Error in event_generator: Object of type bytes is not JSON serializable
Traceback (most recent call last):
File "/Users/stephensmith/src/adk-python/src/google/adk/cli/fast_api.py", line 806, in event_generator
async for event in runner.run_async(
File "/Users/stephensmith/src/adk-python/src/google/adk/runners.py", line 196, in run_async
async for event in invocation_context.agent.run_async(invocation_context):
File "/Users/stephensmith/src/adk-python/src/google/adk/agents/base_agent.py", line 147, in run_async
async for event in self._run_async_impl(ctx):
File "/Users/stephensmith/src/adk-python/src/google/adk/agents/llm_agent.py", line 278, in _run_async_impl
async for event in self._llm_flow.run_async(ctx):
File "/Users/stephensmith/src/adk-python/src/google/adk/flows/llm_flows/base_llm_flow.py", line 279, in run_async
async for event in self._run_one_step_async(invocation_context):
File "/Users/stephensmith/src/adk-python/src/google/adk/flows/llm_flows/base_llm_flow.py", line 305, in _run_one_step_async
async for llm_response in self._call_llm_async(
File "/Users/stephensmith/src/adk-python/src/google/adk/flows/llm_flows/base_llm_flow.py", line 527, in _call_llm_async
trace_call_llm(
File "/Users/stephensmith/src/adk-python/src/google/adk/telemetry.py", line 121, in trace_call_llm
json.dumps(_build_llm_request_for_trace(llm_request)),
File "/Users/stephensmith/.pyenv/versions/3.10.13/lib/python3.10/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/Users/stephensmith/.pyenv/versions/3.10.13/lib/python3.10/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Users/stephensmith/.pyenv/versions/3.10.13/lib/python3.10/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/Users/stephensmith/.pyenv/versions/3.10.13/lib/python3.10/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable

To Reproduce
Steps to reproduce the behavior:

  1. Follow the quickstart in https://google.github.io/adk-docs/get-started/quickstart/
  2. Use this for multi_tool_agent/agent.py:
fromgoogle.adk.agentsimportAgentdefget_bytes_data(city: str) ->bytes:
"""Retrieves bytes data for a specified city. Args: city (str): The name of the city for which to retrieve the bytes data. Returns: bytes: sample bytes data """ifcity.lower() =="new york":
returnb"sample bytes data for new york"else:
returnb"sample bytes data for other cities"root_agent=Agent(
name="sample_bytes_agent",
model="gemini-2.0-flash",
description=(
"Agent to answer questions about sample bytes data for a city."
),
instruction=(
"You are a helpful agent who can answer user questions about the sample bytes data for a city."
),
tools=[get_bytes_data],
)
  1. Run the agent; for example, use the Dev UI via adk web
  2. Ask a question like "What is the sample bytes data for New York?"

Expected behavior
Should not crash. Should skip non-serializable data in tracing functions.

Desktop (please complete the following information):

  • OS: macOS Sequoia 15.4.1
  • Python version(python -V): Python 3.10.13
  • ADK version(pip show google-adk): 1.0.0

Additional context
Spoiler alert: a PR to fix this bug is coming momentarily.

Metadata

Metadata

Labels

core[Component] This issue is related to the core interface and implementationtools[Component] This issue is related to tools

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions