Skip to content

fix(google-genai): preserve Gemini tool-call thought signature and trailing user text order - #2914

Open
SeleneXX wants to merge 2 commits into
MoonshotAI:mainfrom
SeleneXX:fix/gemini-tool-calls
Open

fix(google-genai): preserve Gemini tool-call thought signature and trailing user text order#2914
SeleneXX wants to merge 2 commits into
MoonshotAI:mainfrom
SeleneXX:fix/gemini-tool-calls

Conversation

@SeleneXX

Copy link
Copy Markdown

Related Issue

Resolve #2913

Problem

Gemini (google-genai, e.g. gemini-3.7-flash thinking model) tool-calling sessions fail on the follow-up request with one of two upstream 400 errors:

  1. Function call is missing a thought_signature in functionCall parts. ... — the v2 engine's tool.call loop event drops ToolCall.extras (thought_signature_b64), so the fold-rebuilt assistant message cannot echo thoughtSignature on the outbound functionCall part.
  2. Requests ending with a model turn are not supported.mergeConsecutiveUserMessages merges a trailing plain user message into the preceding tool-result Content as [functionResponse, text]; Gemini 3.x rejects a request whose last Content starts with functionResponse followed by text.

Error #1 masks #2; after fixing #1, long tool-calling sessions then hit #2.

What changed

  • packages/agent-core-v2/src/agent/loop/loopService.ts: pass extras (looked up from response.message.toolCalls) into the tool.call loop event, matching what the v1 engine already does. The event type and the fold already supported the field.
  • packages/kosong/src/providers/google-genai.ts and packages/agent-core-v2/.../google-genai.ts: when merging a tool-result Content with a following text-only user Content, keep the text part first ([text, functionResponse]), which the upstream accepts.

Validation

  • Regression test in packages/agent-core-v2/test/agent/loop/loop.test.ts (extras survive tool.call → fold → context); reverting the fix makes it fail with expected undefined to deeply equal {thought_signature_b64}.
  • Order assertion added to packages/kosong/test/google-genai.test.ts.
  • Replayed a real failing session's wire log through fold → projector → content assembly: trailing Content is now [text, functionResponse, text], accepted by the upstream (200); the pre-fix [functionResponse, text] reproduced the exact 400.
  • kosong 85 tests + agent-core-v2 loop 49 tests pass; both packages typecheck.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset. (changeset included: .changeset/fix-gemini-thought-signature.md)
  • Ran gen-docs skill, or this PR needs no doc update. (no user-facing doc change)

@changeset-bot

changeset-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 42604a9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@moonshot-ai/kimi-code Patch
@moonshot-ai/kimi-code-sdk Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b998a4dd5b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core-v2/src/kosong/provider/bases/google-genai/google-genai.ts Outdated
@SeleneXX
SeleneXX force-pushed the fix/gemini-tool-calls branch from b998a4d to ee17407 Compare August 14, 2026 06:20
@SeleneXX

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ee17407925

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/kosong/src/providers/google-genai.ts Outdated
@SeleneXX
SeleneXX force-pushed the fix/gemini-tool-calls branch from ee17407 to 0182df5 Compare August 14, 2026 06:30
@SeleneXX

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0182df5567

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core-v2/test/agent/loop/loop.test.ts Outdated
@SeleneXX
SeleneXX force-pushed the fix/gemini-tool-calls branch from 0182df5 to 6b860e9 Compare August 14, 2026 06:40
@SeleneXX

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6b860e95e9

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread .changeset/fix-gemini-thought-signature.md
@SeleneXX
SeleneXX force-pushed the fix/gemini-tool-calls branch from 6b860e9 to 2d4cd78 Compare August 14, 2026 07:50
@SeleneXX
SeleneXX force-pushed the fix/gemini-tool-calls branch from 2d4cd78 to 42604a9 Compare August 14, 2026 07:50
@SeleneXX

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 42604a9451

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

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 tool-calling sessions fail on follow-up requests (missing thought_signature / "Requests ending with a model turn")

1 participant