Skip to content

fix: add ordering to recent events in database session service to preserve the chronological sequence of messages: 400 INVALID_ARGUMENT #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

JoaoCampista
Copy link
Contributor

@JoaoCampista JoaoCampista commented Apr 29, 2025

Fixes: #453

Describe the bug
The DatabaseSessionService class (in adk/sessions/database_session_service.py) fetches StorageEvent records without any ORDER BY clause. As a result, events are returned in a non-deterministic order determined by the database’s query planner. When building the context for inference via the Gemini API, messages end up shuffled, leading to hallucinations because the model receives them out of their original sequence.

@JoaoCampista
Copy link
Contributor Author

#453

@JoaoCampista JoaoCampista marked this pull request as draft April 29, 2025 16:29
@JoaoCampista JoaoCampista marked this pull request as ready for review April 29, 2025 16:32
@JoaoCampista JoaoCampista changed the title fix: add ordering to recent events in database session service to preserve the chronological sequence of messages fix: add ordering to recent events in database session service to preserve the chronological sequence of messages: 400 INVALID_ARGUMENT Apr 29, 2025
@JoaoCampista
Copy link
Contributor Author

This resolves the google.genai.errors.ClientError: 400 INVALID_ARGUMENT. {'error': {'code': 400, 'message': 'Please ensure that function call turn comes immediately after a user turn or after a function response turn.', 'status': 'INVALID_ARGUMENT'}}

When retrieve data from db, this get its non cronological, causing inconsistency on order of functions callings e responses.
And reults on 400 Error.

;)

Copy link
Contributor Author

@JoaoCampista JoaoCampista left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Order By

@hangfei hangfei added the ready to pull for importing back to Google label Apr 30, 2025
copybara-service bot pushed a commit that referenced this pull request Apr 30, 2025
--
709e1dd by joao.campista <joaocampista@proton.me>:

feat: add ordering to recent events in database session service
COPYBARA_INTEGRATE_REVIEW=#454 from lugui-co:main 912503f
PiperOrigin-RevId: 753013663
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready to pull for importing back to Google
Projects
None yet
3 participants