Skip to content

fix(model): preserve thought signatures across JSON persistence round-trips - #2910

Merged
jujn merged 8 commits into
agentscope-ai:mainfrom
chaucerj:fix/thought-signature-persistence
Sep 12, 2026
Merged

jujn merged 8 commits into
agentscope-ai:mainfrom
chaucerj:fix/thought-signature-persistence

Conversation

@chaucerj

Copy link
Copy Markdown
Contributor

Fixes #2909

Problem

Gemini 3 requires thought_signature to be replayed with each functionCall. The parsers store signatures as byte[] (and reasoning details as typed OpenAIReasoningDetail) in block metadata, but any JSON persistence round-trip (agent session save/load through JsonUtils/Jackson) restores them as a Base64 String / LinkedHashMap. The converters' instanceof checks silently dropped the values, so the next request went out without signatures and the API rejected it:

400 INVALID_ARGUMENT — "Function call is missing a thought_signature in
functionCall parts. This is required for tools to work correctly for
Gemini 3 models."

Fix

GeminiMessageConverter — besides byte[], accept a non-empty Base64 String signature and decode it back to byte[]; non-Base64 values are skipped with a warning instead of failing.

OpenAIMessageConverter — extract two small normalizers:

  • toSignatureString: accepts both the Base64 String form and the in-memory byte[] form (the latter also fixes the cross-provider path where a Gemini/OpenRouter parser produced byte[] and the OpenAI converter dropped it entirely).
  • toReasoningDetail: accepts the typed OpenAIReasoningDetail and the LinkedHashMap form restored from persistence, converting back via JsonUtils.

Tests

New @Tag("unit") test classes:

  • GeminiThoughtSignaturePersistenceTest — byte[] attached directly; signature restored after JsonUtils toJson/fromJson round-trip (asserts equal bytes); non-Base64 string ignored.
  • OpenAIThoughtSignatureTest — byte[] signature converted to Base64; tool signature + reasoningDetail kept after round-trip; ThinkingBlock reasoning details kept after round-trip.

6/6 new tests pass; the existing GeminiMessageConverterTest + OpenAIMessageConverterTest suites (83 tests) still pass against the patched classes.

Verification

Reproduced end-to-end against the live gemini-3-flash-preview API with two second-turn payloads identical except for the signature:

  • without thoughtSignature: HTTP 400 — "Function call is missing a thought_signature in functionCall parts..."
  • with thoughtSignature: HTTP 200, normal answer.

Before this fix, the round-tripped state produced exactly the first (400) payload.

@chaucerj
chaucerj force-pushed the fix/thought-signature-persistence branch 2 times, most recently from 9daeaf3 to 548dfc1 Compare August 30, 2026 13:28
@codecov

codecov Bot commented Aug 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.22222% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...e/src/main/java/io/agentscope/core/ReActAgent.java 20.00% 6 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

…-trips

Gemini 3 requires thought signatures to be replayed with function calls.
They are stored as byte[] in ToolUseBlock metadata, but a JSON persistence
round-trip (session save/load) restores them as a Base64 String, so the
converters' instanceof checks silently dropped them and the next request
failed with 400 INVALID_ARGUMENT from the server.

- GeminiMessageConverter: decode Base64 String signatures back to byte[]
- OpenAIMessageConverter: normalize byte[] signatures to Base64 String
  (covers the in-memory cross-provider path) and restore reasoning detail
  metadata that comes back from persistence as a LinkedHashMap

Verified against the live gemini-3-flash-preview API: replaying a
function call without its signature returns 400 "Function call is
missing a thought_signature in functionCall parts"; replaying it with
the signature returns 200.

Co-Authored-By: Claude Code <[email protected]>
@chaucerj
chaucerj force-pushed the fix/thought-signature-persistence branch from 548dfc1 to 442b02a Compare August 30, 2026 13:53
chaucerj and others added 3 commits August 30, 2026 21:59
HarnessAgentDynamicHookBuilderTest failed once in agentscope-harness
(own surefire JVM, unrelated to this change; another PR hit the same
1-error-in-844 flake on a different harness test the same day). No code
change.

Co-Authored-By: Claude Code <[email protected]>
Adds a no-metadata ToolUseBlock case to both converter test classes,
exercising the null-metadata guards left as partial branches by the
previous coverage pass. Patch coverage is now 100% of changed lines.

Co-Authored-By: Claude Code <[email protected]>
ToolUseBlock normalizes null metadata to an empty map, so the null-side
of the metadata guards is unreachable and cannot be covered. The actual
partial branches were:

- GeminiMessageConverter: a signature value that is neither byte[] nor
  String (now covered by a non-string signature test)
- OpenAIMessageConverter: a ThinkingBlock details list whose entries
  cannot be restored (now covered), and a dead !candidate.isEmpty()
  check in the fallback-signature scan - toSignatureString never returns
  an empty string, so the check is dropped.

Co-Authored-By: Claude Code <[email protected]>
chaucerj and others added 2 commits August 31, 2026 10:00
…-persistence

Sync with latest main (f96cdc7), including the openai-java 4.52.0 bump.
No code changes; merge was conflict-free.

Co-Authored-By: Claude Code <[email protected]>
…-persistence

Sync with main (b436110): picks up the rag-simple apiKey ambiguity fix
(agentscope-ai#2912) so CI can go green again. No code changes of our own.
Co-Authored-By: Claude Code <[email protected]>

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. fix(model): preserve thought signatures across JSON persistence round-trips — change looks correct and clean.


Automated review by github-manager-bot

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.

Thought signatures lost after JSON persistence round-trip cause 400 INVALID_ARGUMENT on Gemini 3 tool calls

3 participants