Skip to content

Conversation

@Chase-Xuu
Copy link

Description

Fixes #3759

When enable_snapshot_clean is enabled, the _clean_snapshot_in_memory method previously only recreated the FUNCTION message (tool result) but removed both the ASSISTANT message (tool call request) and FUNCTION message from memory.

This breaks Gemini API which requires both messages to exist together - the tool call request must be present alongside the tool call result. Without the ASSISTANT message, Gemini returns the error:
GenerateContentRequest.contents[0].parts[0].function_response.name: Name cannot be empty

Changes

  • Added args and extra_content fields to _ToolOutputHistoryEntry to preserve tool call context for later reconstruction
  • Modified _register_tool_output_for_cache to accept and store args/extra_content
  • Modified _clean_snapshot_in_memory to recreate BOTH the ASSISTANT message (with tool_calls) and the FUNCTION message (with result) when cleaning snapshots
  • Updated _record_tool_calling to pass args and extra_content to the cache registration function

Testing

This fix ensures proper tool call message reconstruction that is compatible with Gemini and other APIs that require paired tool call request/response messages.

The fix is based on @szhengac's investigation and initial fix, properly integrated and refined for the current codebase.

Checklist

  • I have read the Contributing Guidelines
  • I have linked relevant issues in the PR description
  • I have verified my changes with syntax checking

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 10, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Collaborator

@waleedalzarooni waleedalzarooni left a comment

Choose a reason for hiding this comment

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

Great job @Chase-Xuu,

Just a few comments on my side!

func_timestamp = assist_timestamp + 1e-6

# Recreate assistant message (tool call request)
cleaned_assist_message = FunctionCallingMessage(
Copy link
Collaborator

Choose a reason for hiding this comment

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

The recreated assistant message in _clean_snapshot_in_memory uses FunctionCallingMessage with args/func_name fields and an empty meta_dict, but the original message recorded _record_assistant_tool_calls_from_requests is a BaseMessage with tool calls in meta_dict["tool_calls"].

This format mismatch means the cleaned message serializes differently than the original, and doesn't support multi-tool-call responses, the original stores all tool calls from one model response in a single message, while the recreation produces one message per tool call.

class _ToolOutputHistoryEntry:
tool_name: str
tool_call_id: str
result_text: str
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would be great if you could include some tests to verify the changes

…pshot clean for Gemini API

Fixes camel-ai#3759

When `enable_snapshot_clean` is enabled, the `_clean_snapshot_in_memory` method
previously only recreated the FUNCTION message (tool result) but removed both
the ASSISTANT message (tool call request) and FUNCTION message from memory.

This breaks Gemini API which requires both messages to exist together - the tool
call request must be present alongside the tool call result. Without the ASSISTANT
message, Gemini returns the error:
`GenerateContentRequest.contents[0].parts[0].function_response.name: Name cannot be empty`

Changes:
- Added `args` and `extra_content` fields to `_ToolOutputHistoryEntry` to preserve
  tool call context for later reconstruction
- Modified `_register_tool_output_for_cache` to accept and store args/extra_content
- Modified `_clean_snapshot_in_memory` to recreate BOTH the ASSISTANT message
  (with tool_calls) and the FUNCTION message (with result) when cleaning snapshots
- Updated `_record_tool_calling` to pass args and extra_content to the cache
  registration function

This ensures proper tool call message reconstruction that is compatible with
Gemini and other APIs that require paired tool call request/response messages.
Address reviewer feedback:
- Remove unnecessary recreation of assistant message (already recorded
  separately by _record_assistant_tool_calls_from_requests)
- Fixes format mismatch issue (FunctionCallingMessage vs BaseMessage)
- Fixes multi-tool-call support (assistant message preserves all tool calls)
- Add test for snapshot cleaning functionality
@Chase-Xuu Chase-Xuu force-pushed the fix/gemini-tool-call-snapshot-clean-3759 branch from 74d2418 to 3eef580 Compare February 10, 2026 17:00
@Chase-Xuu
Copy link
Author

Thanks for the review feedback @waleedalzarooni!

I've addressed both points in commit 3eef580:

  1. Format mismatch & multi-tool-call support: Simplified the approach - instead of recreating the assistant message (which caused the FunctionCallingMessage vs BaseMessage mismatch), I now rely on the original assistant message that's already recorded by _record_assistant_tool_calls_from_requests. The _clean_snapshot_in_memory method now only updates the function result message, preserving the original assistant message format and multi-tool-call support.

  2. Tests: Added tests in test/agents/test_chat_agent.py to verify the snapshot cleaning functionality works correctly with tool calls.

Please let me know if you'd like any other changes!

@waleedalzarooni
Copy link
Collaborator

Hey @Chase-Xuu, thanks for the switch but I believe the current PR is now only implementing a refactor which won't solve the original problem. Would you mind having another quick look to ensure the original problem is fixed, I'll also have a look to see how to best implement this.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] GEMINI with Tool Calls

2 participants