Add image_generation server-side tool - #186
Merged
Merged
Conversation
yanxi0830
force-pushed
the
feature/image-generation-tool
branch
from
August 15, 2026 00:48
8976d44 to
4a051bd
Compare
mark-xai
approved these changes
Aug 15, 2026
Adds xai_sdk.tools.image_generation() for the server-side image_generation
tool, enabling image generation and editing in agentic requests. Supports an
optional action parameter ("auto", "generate", "edit").
Regenerates v5/v6 chat and usage protos with the ImageGeneration tool message,
TOOL_CALL_TYPE_IMAGE_GENERATION_TOOL, and SERVER_SIDE_TOOL_IMAGE_GENERATION.
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
yanxi0830
force-pushed
the
feature/image-generation-tool
branch
from
August 17, 2026 00:03
4a051bd to
d845bb5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds the
image_generationserver-side (built-in) tool to the SDK, following theweb_search/x_search/code_executionagentic-tool pattern, along with aResponse.image_outputsproperty for retrieving the generated images.Changes
image_generation()tool helpertools.py: Newimage_generation()helper that returns achat_pb2.Tool. Accepts an optionalactionparameter —"auto"(default; both generation and editing),"generate"(text-to-image only), or"edit"(image editing only). Also documents"image_generation_tool"as a possible return value ofget_tool_call_type().ImageGenerationmessage andimage_generationmember in theTooloneof,TOOL_CALL_TYPE_IMAGE_GENERATION_TOOLinToolCallType(chat_pb2), andSERVER_SIDE_TOOL_IMAGE_GENERATIONinServerSideTool(usage_pb2) for usage tracking.Response.image_outputsRetrieving generated images previously required hand-parsing the
ROLE_TOOLoutput content: JSON-decode the{"__type": "image_generation_result", ...}envelope, split the data URL, and base64-decode the payload. This PR adds anImageGenerationOutputwrapper and aResponse.image_outputsproperty (mirroring the existingoutput_filespattern), so callers can simply do:Response.image_outputs— one entry per completed image generation/edit call, in response order. Failed calls carry an error payload instead of an image envelope and are omitted; the property returns an empty sequence when no images were generated.ImageGenerationOutput— parses the result envelope once and exposes:image: decoded raw bytesmime_type/data_url: self-describing payload metadataimage_uuid: reference ID for follow-up edits (empty on older envelopes, matching the server's#[serde(default)])tool_call: the originating completed tool call (imagine_text_to_imagevsimagine_image_to_image)Works for sync and aio clients, and for both
sample()andstream()— the accumulated streaming response syncs its content buffers before parsing.Examples and tests
examples/{sync,aio}/image_generation_tool.py— single-turn generation, editing an attached input image via theimage()content helper, multi-turn generate-then-edit in a single chat viachat.append(response), web_search + image_generation combo, and verbose streaming, all built on the new API (the existingimage_generation.pyexamples only cover the standaloneclient.imageAPI).actionvalue), tool-call type mapping, usage enum, plus sync/aio × sample/streamimage_outputstests, failed-call omission, and malformed-envelope handling.CHANGELOG.mdentries.Usage
Test plan
uv run ruff format --checkanduv run ruff checkuv run pyright— 0 errorsuv run pytest -n auto— 772 passedMade with Cursor