Skip to content

feat(genai): add support for thought_signature in image content blocks#1838

Open
Maciej Nachtygal (nhtgl) wants to merge 1 commit into
langchain-ai:mainfrom
nhtgl:main
Open

feat(genai): add support for thought_signature in image content blocks#1838
Maciej Nachtygal (nhtgl) wants to merge 1 commit into
langchain-ai:mainfrom
nhtgl:main

Conversation

@nhtgl

Copy link
Copy Markdown

Description

Gemini image generation models return a thought_signature alongside generated images. This signature must be sent back in subsequent requests for multi-turn image refinement — without it, the API rejects the request with INVALID_ARGUMENT: Image part is missing a thought_signature.

Two bugs prevented this from working:

  1. thought_signature on image Parts was not exposed through AIMessage.content_blocks (it was being dropped during parsing)
  2. Even when extras.signature was present on a content block, _convert_to_parts had a pass stub instead of actually setting thought_signature on the outgoing Part

Both paths are now fixed: inline base64 images and file URI images correctly round-trip their thought_signature through content_blocks.

Relevant issues

#1837

Type

🐛 Bug Fix

Changes(optional)

  • _compat.py: preserve thought_signature from extras.signature when converting content blocks back to Google GenAI format (both inline inline_data and file_data paths)
  • chat_models.py: wire thought_signature into Part kwargs for image blocks (was a pass stub)
  • Tests: roundtrip tests via content_blocks for both base64 and file URI image blocks; direct _convert_to_parts unit tests for both image block types

Testing(optional)

New unit tests in tests/unit_tests/test_chat_models.py:

  • test_image_signature_roundtrip_via_content_blocks
  • test_image_file_uri_signature_roundtrip_via_content_blocks
  • test_convert_to_parts_image_base64_thought_signature
  • test_convert_to_parts_image_url_thought_signature
    Integration tested manually against gemini-3-pro-image using the repro script from the issue — Turn 2 now succeeds.

Note(optional)

The issue also mentions skip_thought_signature_validator as a fallback. This PR does not add that — if the signature is present in content_blocks, it will be sent; if not (e.g. older messages), the caller is responsible. Addressing the validator flag is a separate concern.

AI involvement: This PR was developed with the assistance of Claude Code (Anthropic).

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.

1 participant