Skip to content

Tool execution succeeds but agent crashes due to serialization of unused tool variable #1056

Description

@Spixz

Describe the bug
When my agent calls the tool, something try to serialize a variable from the tool that is not actually used, which causes the agent to crash. However, the tool itself is executed correctly.

File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/pydantic/type_adapter.py", line 572, in dump_python
return self.serializer.to_python(
^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'bson.objectid.ObjectId'>

To Reproduce

fromgoogle.adk.agentsimportLlmAgentfrompymongoimportMongoClientimportosdefstore_events_in_db(events: list[dict]) ->str:
"""Insert a list of event dicts into MongoDB."""try:
client=MongoClient(os.getenv("MONGO_URI"))
db=client["ww1-france"]
coll=db["events"]
status=coll.insert_many(events)
# coll.insert_many(events) crash tooreturn"Events stored successfully."exceptExceptionase:
returnf"Error storing events: {e}"root_agent=LlmAgent(
name="StoreEventsAgent",
model="gemini-2.0-flash",
instruction="""You are given a variable `{events?}` containing a list of event dictionaries.Call the tool `store_events_in_db` to insert `{events?}` into the database.""",
description="Store the given list of events in MongoDB",
tools=[store_events_in_db],
output_key="store_event_agent_output",
)
# mongo.Colleciton.insert_many() -> InsertManyResult:# """Insert an iterable of documents.# >>> result = db.test.insert_many([{'x': i} for i in range(2)])# >>> result.inserted_ids# [ObjectId('54f113fffba522406c9cc20e'), ObjectId('54f113fffba522406c9cc20f')]

The line causing the error is the following status = coll.insert_many(events).

  • Create a MongoDB database and set the MONGO_URI environment variable.
  • Run the agent using adk run web, then ask it the following:

add [{"title": "insert from chat 12"}] to events and insert it in the database.

(The same error appear when the agent is launch with runner.run)

The tool executes correctly, and the data is properly stored in the database, but the following error appears:
Image

The main part of the error

 File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/routing.py", line 73, in app
response = await f(request)
^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/fastapi/routing.py", line 327, in app
content = await serialize_response(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/fastapi/routing.py", line 181, in serialize_response
return field.serialize(
^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/fastapi/_compat.py", line 151, in serialize
return self._type_adapter.dump_python(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/pydantic/type_adapter.py", line 572, in dump_python
return self.serializer.to_python(
^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'bson.objectid.ObjectId'>
build graph

Complete logs

LLM Request:
-----------------------------------------------------------
System Instruction:
You are given a variable `` containing a list of event dictionaries.
Call the tool `store_events_in_db` to insert `` into the database.
You are an agent. Your internal name is "StoreEventsAgent".
The description about you is "Store the given list of events in MongoDB"
-----------------------------------------------------------
Contents:
{"parts":[{"text":"add [{\"title\": \"insert from tchat 12\"}] to events and insert it in the database."}],"role":"user"}
-----------------------------------------------------------
Functions:
store_events_in_db: {'events': {'items': {'type': <Type.OBJECT: 'OBJECT'>}, 'type': <Type.ARRAY: 'ARRAY'>}} -> None
-----------------------------------------------------------
2025-05-30 16:37:21,123 - INFO - models.py:7118 - AFC is enabled with max remote calls: 10.
2025-05-30 16:37:22,027 - INFO - _client.py:1740 - HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent "HTTP/1.1 200 OK"
2025-05-30 16:37:22,030 - WARNING - types.py:4572 - Warning: there are non-text parts in the response: ['function_call'], returning concatenated text result from text parts. Check the full candidates.content.parts accessor to get the full model response.
2025-05-30 16:37:22,032 - INFO - google_llm.py:151 - LLM Response:
-----------------------------------------------------------
Text:
None
-----------------------------------------------------------
Function calls:
name: store_events_in_db, args: {'events': [{'title': 'insert from tchat 12'}]}
-----------------------------------------------------------
Raw response:
{"candidates":[{"content":{"parts":[{"function_call":{"args":{"events":[{"title":"insert from tchat 12"}]},"name":"store_events_in_db"}}],"role":"model"},"finish_reason":"STOP","avg_logprobs":-6.352143827825785e-6}],"model_version":"gemini-2.0-flash","usage_metadata":{"candidates_token_count":16,"candidates_tokens_details":[{"modality":"TEXT","token_count":16}],"prompt_token_count":109,"prompt_tokens_details":[{"modality":"TEXT","token_count":109}],"total_token_count":125},"automatic_function_calling_history":[]}
-----------------------------------------------------------
2025-05-30 16:37:22,033 - INFO - fast_api.py:705 - Generated event in agent run streaming: {"content":{"parts":[{"functionCall":{"id":"adk-fa88b514-8d96-4fdc-ae0a-7df90f257425","args":{"events":[{"title":"insert from tchat 12"}]},"name":"store_events_in_db"}}],"role":"model"},"usageMetadata":{"candidatesTokenCount":16,"candidatesTokensDetails":[{"modality":"TEXT","tokenCount":16}],"promptTokenCount":109,"promptTokensDetails":[{"modality":"TEXT","tokenCount":109}],"totalTokenCount":125},"invocationId":"e-9b8c827e-3fef-4e1a-96d9-e70f81e3c851","author":"StoreEventsAgent","actions":{"stateDelta":{},"artifactDelta":{},"requestedAuthConfigs":{}},"longRunningToolIds":[],"id":"FAZtPJCR","timestamp":1748615841.093331}
2025-05-30 16:37:26,575 - INFO - fast_api.py:705 - Generated event in agent run streaming: {"content":{"parts":[{"functionResponse":{"id":"adk-fa88b514-8d96-4fdc-ae0a-7df90f257425","name":"store_events_in_db","response":{"result":"Events stored successfully."}}}],"role":"user"},"invocationId":"e-9b8c827e-3fef-4e1a-96d9-e70f81e3c851","author":"StoreEventsAgent","actions":{"stateDelta":{},"artifactDelta":{},"requestedAuthConfigs":{}},"id":"qQsJvuYT","timestamp":1748615846.574966}
2025-05-30 16:37:26,616 - INFO - google_llm.py:85 - Sending out request, model: gemini-2.0-flash, backend: ml_dev, stream: False
2025-05-30 16:37:26,622 - ERROR - fast_api.py:708 - Error in event_generator: Unable to serialize unknown type: <class 'bson.objectid.ObjectId'>
Traceback (most recent call last):
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/google/adk/cli/fast_api.py", line 697, in event_generator
async for event in runner.run_async(
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/google/adk/runners.py", line 197, in run_async
async for event in invocation_context.agent.run_async(invocation_context):
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/google/adk/agents/base_agent.py", line 147, in run_async
async for event in self._run_async_impl(ctx):
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/google/adk/agents/llm_agent.py", line 278, in _run_async_impl
async for event in self._llm_flow.run_async(ctx):
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/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/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/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/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 520, in _call_llm_async
async for llm_response in llm.generate_content_async(
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/google/adk/models/google_llm.py", line 91, in generate_content_async
logger.info(_build_request_log(llm_request))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/google/adk/models/google_llm.py", line 253, in _build_request_log
content.model_dump_json(
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/pydantic/main.py", line 519, in model_dump_json
return self.__pydantic_serializer__.to_json(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'bson.objectid.ObjectId'>
INFO: 127.0.0.1:56457 - "GET /apps/test_issue_sample/users/user/sessions/e81eea60-6a6d-4e3e-ad0f-a93069993f0c HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/fastapi/applications.py", line 1054, in __call__
await super().__call__(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 187, in __call__
raise exc
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 165, in __call__
await self.app(scope, receive, _send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/routing.py", line 714, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/routing.py", line 734, in app
await route.handle(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/routing.py", line 288, in handle
await self.app(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/routing.py", line 76, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/routing.py", line 73, in app
response = await f(request)
^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/fastapi/routing.py", line 327, in app
content = await serialize_response(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/fastapi/routing.py", line 181, in serialize_response
return field.serialize(
^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/fastapi/_compat.py", line 151, in serialize
return self._type_adapter.dump_python(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/pydantic/type_adapter.py", line 572, in dump_python
return self.serializer.to_python(
^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'bson.objectid.ObjectId'>
INFO: 127.0.0.1:56468 - "GET /debug/trace/session/e81eea60-6a6d-4e3e-ad0f-a93069993f0c HTTP/1.1" 200 OK
INFO: 127.0.0.1:56472 - "GET /apps/test_issue_sample/users/user/sessions/e81eea60-6a6d-4e3e-ad0f-a93069993f0c HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/fastapi/applications.py", line 1054, in __call__
await super().__call__(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 187, in __call__
raise exc
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 165, in __call__
await self.app(scope, receive, _send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/routing.py", line 714, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/routing.py", line 734, in app
await route.handle(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/routing.py", line 288, in handle
await self.app(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/routing.py", line 76, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/starlette/routing.py", line 73, in app
response = await f(request)
^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/fastapi/routing.py", line 327, in app
content = await serialize_response(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/fastapi/routing.py", line 181, in serialize_response
return field.serialize(
^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/fastapi/_compat.py", line 151, in serialize
return self._type_adapter.dump_python(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/cyril/projets/ww1_map/.venv/lib/python3.12/site-packages/pydantic/type_adapter.py", line 572, in dump_python
return self.serializer.to_python(
^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'bson.objectid.ObjectId'>
build graph
INFO: 127.0.0.1:56474 - "GET /apps/test_issue_sample/users/user/sessions/e81eea60-6a6d-4e3e-ad0f-a93069993f0c/events/qQsJvuYT/graph HTTP/1.1" 200 OK
INFO: 127.0.0.1:56473 - "GET /debug/trace/qQsJvuYT HTTP/1.1" 200 OK

Expected behavior
Should not crash.

Desktop

  • OS: MacOS Sequioa 15.4.1
  • Python version: 3.12.1
  • ADK version: 1.1.1

Additional context
The most similar issue found : #878
Thanks for your work.

Metadata

Metadata

Assignees

Labels

tools[Component] This issue is related to tools

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions