Add image_generation server-side tool - #181
Closed
yanxi0830 wants to merge 2 commits into
Closed
Conversation
Adds the xai_sdk.tools.image_generation() helper for the server-side
image_generation tool, enabling image generation and editing in agentic
requests. The optional action parameter ("auto", "generate", or "edit")
controls which image capabilities are exposed to the model.
Regenerates the v5/v6 chat and usage protos to add the ImageGeneration
tool message, TOOL_CALL_TYPE_IMAGE_GENERATION_TOOL,
SERVER_SIDE_TOOL_IMAGE_GENERATION, and the num_image_generations /
num_image_edits usage fields.
Co-authored-by: Cursor <cursoragent@cursor.com>
Adds sync and async examples showing how to use the server-side image_generation tool in agentic requests: generating an image, decoding the ROLE_TOOL result envelope to save it to disk, streaming, and multi-turn editing via previous_response_id. Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Summary
Adds the server-side
image_generationtool to the SDK, mirroring the tool now supported by the xAI API (following theweb_search/x_search/code_executionagentic-tool pattern):xai_sdk.tools.image_generation(): new helper that returns achat_pb2.Toolfor image generation and editing in agentic requests. Takes an optionalactionparameter —"auto"(default; generate + edit),"generate"(text-to-image only), or"edit"(image editing only). Omittingactionleaves the field unset so the server applies its default.chat.proto:ImageGenerationmessage (optional string action = 1),image_generation = 10in theTooloneof, andTOOL_CALL_TYPE_IMAGE_GENERATION_TOOL = 10.usage.proto:SERVER_SIDE_TOOL_IMAGE_GENERATION = 11andnum_image_generations/num_image_editsfields onSamplingUsagefor per-call generation/edit billing visibility.get_tool_call_type()docstring now listsimage_generation_toolas a possible return value.examples/sync/image_generation_tool.pyandexamples/aio/image_generation_tool.pyshowing generation, decoding theROLE_TOOLresult envelope ({"__type": "image_generation_result", "result": "data:<mime>;base64,..."}) to save the image to disk, streaming, and multi-turn editing viaprevious_response_id.Usage:
Test plan
uv run ruff format --checkpassesuv run ruff checkpassesuv run pyrightpasses (0 errors)uv run pytest -n auto— all 770 tests passimage_generation()default and per-action proto construction,TOOL_CALL_TYPE_IMAGE_GENERATION_TOOLmapping viaget_tool_call_type,SERVER_SIDE_TOOL_IMAGE_GENERATIONenum value,SamplingUsageimage fields, and sync/asyncchat.create(tools=[...])round-tripsprotobuf==5.29.4, and v6 underprotobuf==6.31.1imagine_text_to_image/imagine_image_to_imagewithTOOL_CALL_TYPE_IMAGE_GENERATION_TOOL, theROLE_TOOLenvelope decodes to a valid image, usage reportsSERVER_SIDE_TOOL_IMAGE_GENERATIONwithnum_image_generations/num_image_editspopulated, and both example scripts (sync generate, multi-turn edit, async streaming) run successfully