Skip to content

DatabaseSessionService error : Database Column Length Exceeded for Error Messages #3351

Closed

Description

@prasadskarmarkar

Describe the bug
When using DatabaseSessionService with PostgreSQL as a persistent database for session storage, error messages that exceed the database column length limit (VARCHAR(1024)) cause a StringDataRightTruncation error. Instead of gracefully handling this error, the entire agent execution crashes, preventing proper error reporting and potentially leaving the session in an inconsistent state.

To Reproduce
Steps to reproduce the behavior:

  1. Set up a PostgreSQL database for session persistence
  2. Configure DatabaseSessionService with the PostgreSQL connection:
from google.adk.sessions import DatabaseSessionService
from google.adk import Runner

# Using PostgreSQL for persistent session storage
session_service = DatabaseSessionService(
    db_url="postgresql://user:password@host:5432/database"
)

# Agent configured with persistent session service
runner = Runner(
    agent=agent,
    session_service=session_service
)
  1. Run an agent that involves complex data processing (e.g., plot_agent with large datasets)
  2. Trigger a MALFORMED_FUNCTION_CALL error with a large payload
  3. Observe the database error and agent crash with this error:
Error: Agent API error: {"error": "(psycopg2.errors.StringDataRightTruncation) value too long for type character varying(1024)\n\n[SQL: INSERT INTO events (...) VALUES (...)]\n[parameters: {..., 'error_message': 'Malformed function call: print(default_api.plot_agent(request='''Create a line graph with this data: [{'transaction_day': '2024-10-24', 'dail ... (40424 characters truncated) ...)', 'interrupted': None}]\n(Background on this error at: https://sqlalche.me/e/20/9h9h)", "timestamp": "2025-10-29T13:00:34.724692", "request_id": "req_1761767910262", "user_id": "2", "session_id": "2025_10_29_19_37_15_A9AA"}

Expected behavior
The agent should handle database insertion errors gracefully and continue operation, rather than crashing when error messages exceed the column size limit. Error logging failures should not cause agent execution failures.

Screenshots
N/A

Desktop (please complete the following information):

  • OS: Linux/Ubuntu
  • Python version: 3.11+
  • ADK version: 1.15.1

Model Information:

  • Are you using LiteLLM: No
  • Which model is being used: gemini-2.5-flash ( google-adk running in GKE cluster )

Additional context

Root Cause Analysis:

  1. The events table in the PostgreSQL database used by DatabaseSessionService defines error_message as VARCHAR(1024)
  2. When using data-intensive agents (especially with plot_agent), error messages can exceed 40,000 characters
  3. DatabaseSessionService does not handle PostgreSQL insertion errors, so the column length violation crashes the agent execution

Current Impact:

  • Affects all production deployments using DatabaseSessionService with PostgreSQL
  • Particularly impacts data-intensive and visualization agents
  • Results in agent crashes and potential session state corruption in PostgreSQL
  • 97%+ of error context is lost due to truncation

Technical Details:

  • Failed SQL Operation:
INSERT INTO events (
    id, app_name, user_id, session_id, invocation_id, author, 
    actions, long_running_tool_ids_json, branch, timestamp, 
    content, grounding_metadata, custom_metadata, 
    partial, turn_complete, error_code, error_message, interrupted
) VALUES (...)
  • Problematic Field: error_message (VARCHAR(1024))
  • Actual Length in Example: ~40,424 characters
  • Triggers: plot_agent, large data payloads, MALFORMED_FUNCTION_CALL errors

Temporary Workaround:
Until fixed, users can manually alter their PostgreSQL database:

ALTER TABLE events ALTER COLUMN error_message TYPE TEXT;

Proposed Solutions:

  1. Add error handling in DatabaseSessionService to prevent crashes when PostgreSQL logging fails
  2. Implement smart truncation for oversized error messages
  3. Update the PostgreSQL schema to use TEXT type instead of VARCHAR(1024)
  4. Add configuration options for max error message length

Dependencies:

  • SQLAlchemy 2.0.43
  • asyncpg 0.30.0 (async PostgreSQL driver)
  • psycopg2-binary 2.9.11 (sync PostgreSQL driver)
  • fastapi==0.118.0
  • pydantic==2.11.9
  • uvicorn==0.37.0

Related Files:

  • google/adk/sessions/database_session_service.py

Activity

  1. added
    services[Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc
    on Nov 1, 2025
  2. added theissue type on Nov 1, 2025
  3. prasadskarmarkar commented on Nov 1, 2025

    @prasadskarmarkar
    Author

    ** implemented the following temporary fix in our Agent execution environment , as a workaround **

    ALTER TABLE events ALTER COLUMN error_message TYPE varchar(10000);
  4. surajksharma07 commented on Nov 3, 2025

    @surajksharma07
    Collaborator

    @prasadskarmarkar the error_message column at line 270 in database_session_service.py is defined as String(1024) which is too small for real-world error messages, especially MALFORMED_FUNCTION_CALL errors with large data payloads.
    The fix is straightforward: change to Text type (like other large fields in the schema) and add error handling around the commit to prevent crashes.
    Thanks for the detailed analysis and for applying the workaround - this helps production deployments stay stable.

  5. prasadskarmarkar commented on Nov 7, 2025

    @prasadskarmarkar
    Author

    how are we planning to fix this bug ?

    Here is my analysis :

    In this file src/google/adk/sessions/database_session_service.py

    The SQLAlchemy create_all() method (called on line 454: Base.metadata.create_all(self.db_engine)) only creates tables and columns that don't exist. It doesn't modify existing column definitions

    As per SQLAlchemy documentation :

    SQLAlchemy's create_all() method:

    1. Creates tables that don't exist yet
    2. Does NOT modify existing tables
    3. Does NOT change column types or lengths of existing columns
    4. Does NOT delete columns that are no longer in the model
    5. Does NOT add constraints to existing columns

    This means that if we change a column type from String(1024) to Text in the database_session_service.py file , and the table already exists in the database, SQLAlchemy will NOT alter the column type in the database. It will only use the new definition when creating new tables.

    Just changing the datatype from String(1024) to TEXT in your code will not alter the column in the database if an ADK agent with DatabaseSessionService is already running in a production environment.

    Since only a small percentage of deployments might have this issue, we recommend:

    1. Document the limitation in your release notes, clearly stating that changing String(1024) to Text in the model won't modify existing database columns.
    2. Provide a simple Alembic migration script that users can run if needed, as it's the most straightforward solution for Python/SQLAlchemy applications.
    3. Consider adding a validation check on startup that warns users if their database schema doesn't match the expected model definitions.

    Let me know your thoughts.

  6. changed the title [-]DatabaseSessionService error : Database Column Length Exceeded for Error Messages in Sequential Agent Workflows[/-] [+]DatabaseSessionService error : Database Column Length Exceeded for Error Messages[/+] on Nov 9, 2025
  7. mattmcc-attest commented on Nov 20, 2025

    @mattmcc-attest

    also see this quite a lot

  8. surajksharma07 commented on Nov 20, 2025

    @surajksharma07
    Collaborator

    Thanks @mattmcc-attest for confirming this is widespread.
    @prasadskarmarkar you're absolutely right about the SQLAlchemy create_all() limitation - just changing the code to Text won't modify existing columns.
    We are looking more into your recommended approach: (1) change schema to Text in code,
    (2) provide an Alembic migration script users can run,
    (3) document in release notes that existing deployments need manual migration, and
    (4) add startup validation to warn if schema doesn't match.
    This is the right way to handle it while respecting that only a subset of deployments are affected.

  9. added
    need attention[Status] This issue is important but the maintainers haven't started working on it
    on Nov 20, 2025
  10. surajksharma07 commented on Nov 20, 2025

    @surajksharma07
    Collaborator

    @DeanChensj Please have a look into it.

  11. DeanChensj commented on Nov 27, 2025

    @DeanChensj
    Collaborator

    Hi @prasadskarmarkar , thanks for reporting this issue, in 0094eea we migrated the db schema to use json payload to represent the event object. As a side effect this issue should also be fixed. You should be able to migrate to a new DB with adk migrate session, in place db migration is not supported though

  12. removed
    need attention[Status] This issue is important but the maintainers haven't started working on it
    on Nov 27, 2025
  13. prasadskarmarkar commented on Nov 28, 2025

    @prasadskarmarkar
    Author

    @DeanChensj

    The fix looks good.

    A note regarding deployment:
    The adk migrate session command cannot be executed from within our production GKE application pods, unlike in local environments.

    Production GKE Considerations

    Most production pods do not include the ADK CLI and do not allow ad-hoc command execution.
    The schema migration must be performed once per environment before deploying the updated ADK version.

    Required Approach

    We will need to run the migration using a one-off Kubernetes Job in the production GKE cluster with the appropriate DB credentials.
    This does introduce additional operational overhead for teams running services on their organization’s custom GKE cluster, good to document the same.

  14. mattmcc-attest commented on Dec 3, 2025

    @mattmcc-attest

    @DeanChensj what would you recommend we do for in production databases where we want to update in the schema in place?

  15. DeanChensj commented on Dec 3, 2025

    @DeanChensj
    Collaborator

    Hi @mattmcc-attest , what about you migrate from DB A to DB B, and then migrate back to DB A?

  16. prasadskarmarkar commented on Dec 3, 2025

    @prasadskarmarkar
    Author

    I wrote a much cleaner implementation of DB migration so that the code ( migration manager, takes care of the db migration as a part of code execution , no additional adk commands or scripts needed ) but did not create a pull request as the schema totally changed.

    but looks like you have reverted these changes now : 9d918d4#diff-529a63417090757c651b62a1f94b04f86b27413415b55a7304637e3f10f66b25

    does this mean the bug is still open ?

    i will test my changes with the latest code and share my pull request for this bug fix. let me know your thoughts...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

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