-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Description
When using DatabaseSessionService (or any persistent session service), append_event crashes with PydanticSerializationError because the ADK Runner attaches non-serializable function objects to events before calling session_service.append_event().
The issue is in Runner._append_new_message_to_session ([runners.py:880](http://runners.py:880/)) which passes events containing function references to session_service.append_event(). The DatabaseSessionService then calls event.model_dump(exclude_none=True, mode="json") in StorageEvent.from_event() (schemas/[v1.py:194](http://v1.py:194/)), which fails on the non-serializable function objects.
This only affects persistent session services. InMemorySessionService works because it never serializes events.
Steps to Reproduce
- Create an agent with tools (e.g., MCP tools via
McpToolset) - Use
DatabaseSessionServicewith PostgreSQL:
from google.adk.sessions.database_session_service import DatabaseSessionService
from google.adk.runners import Runner
runner = Runner(
app_name="my-agent",
agent=my_agent,
session_service=DatabaseSessionService(
db_url="postgresql+asyncpg://localhost:5432/mydb"
),
artifact_service=InMemoryArtifactService(),
memory_service=InMemoryMemoryService(),
credential_service=InMemoryCredentialService(),
)- Send a message that triggers tool execution
- Agent crashes on the first
append_eventcall
Error Traceback
pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'function'>
File "google/adk/[runners.py](http://runners.py/)", line 880, in _append_new_message_to_session
await self.session_service.append_event(session=session, event=event)
File "google/adk/sessions/database_session_service.py", line 476, in append_event
sql_session.add(schema.StorageEvent.from_event(session, event))
File "google/adk/sessions/schemas/[v1.py](http://v1.py/)", line 194, in from_event
event_data=event.model_dump(exclude_none=True, mode="json"),
pydantic_core._pydantic_core.PydanticSerializationError:
Unable to serialize unknown type: <class 'function'>
Root Cause
StorageEvent.from_event() in schemas/[v1.py:194](http://v1.py:194/) calls:
event_data=event.model_dump(exclude_none=True, mode="json")This does not use a fallback parameter, so any non-serializable type (like function objects attached by the Runner during tool resolution) causes a hard crash. Pydantic v2 supports a fallback parameter in model_dump() / model_dump_json() that can gracefully handle unknown types.
Environment
- google-adk: 1.22.1 (also confirmed on latest main branch / 1.26.0)
- Python: 3.13
- Pydantic: 2.12.5
- Database: PostgreSQL via
asyncpg - Tools: MCP tools via
McpToolset/streamablehttp_client
Impact
- Severity: High - Any agent using persistent session services with tools will crash
- Scope: Affects
DatabaseSessionService, and any custom persistent session service implementation - Affected versions: All versions (1.0.0 through 1.26.0)
Related Issues
-
Tool execution succeeds but agent crashes due to serialization of unused tool variable #1056 - Tool execution succeeds but agent crashes due to serialization of unused tool variable
-
Unable to serialize Async generator type for streaming tool #960 - Unable to serialize Async generator type for streaming tool
-
EventCompaction deserialized as dict instead of Pydantic model in DatabaseSessionService #3633 - EventCompaction deserialized as dict instead of Pydantic model in DatabaseSessionService
Model Information: -
Are you using LiteLLM: Yes
-
Which model is being used: azure/gpt-5.1-chat