Skip to content

DatabaseSessionService.append_event crashes with PydanticSerializationError: Unable to serialize unknown type: <class 'function'> #4724

@varun-official

Description

@varun-official

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

  1. Create an agent with tools (e.g., MCP tools via McpToolset)
  2. Use DatabaseSessionService with 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(),
)
  1. Send a message that triggers tool execution
  2. Agent crashes on the first append_event call

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    services[Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions