Skip to content

chat: image() rejects an unknown detail value instead of sending auto - #239

Open
deanrie wants to merge 1 commit into
xai-org:mainfrom
deanrie:image-detail-validation
Open

deanrie wants to merge 1 commit into
xai-org:mainfrom
deanrie:image-detail-validation

Conversation

@deanrie

@deanrie deanrie commented Oct 24, 2026

Copy link
Copy Markdown

What

xai_sdk.chat.image(image_url, *, detail="auto") maps "low" and "high" and lets anything else fall through to DETAIL_AUTO:

pb_detail = image_pb2.ImageDetail.DETAIL_AUTO
if detail == "low":
    pb_detail = image_pb2.ImageDetail.DETAIL_LOW
elif detail == "high":
    pb_detail = image_pb2.ImageDetail.DETAIL_HIGH

So image(url, detail="hgh") or detail="HIGH" sends the image at auto resolution with no signal to the caller. Every other literal in the chat API raises on a value outside its Literal: _reasoning_effort_to_proto, _tool_mode_to_proto, _format_type_to_proto, _include_option_to_proto, _agent_count_to_proto, service_tier_to_proto, SearchParameters._search_mode_to_proto, and the six convert_image_*_to_pb / convert_video_*_to_pb helpers. image() is the one exception.

Change

_image_detail_to_proto matches None | "auto", "low", "high" and raises ValueError("Invalid image detail: 'hgh'. Must be one of: ['auto', 'low', 'high']") otherwise, the same shape as the other converters. None keeps meaning auto, since the signature is Optional[ImageDetail]. The three valid values produce the same protos as before; test_chat_append_with_images in the sync and aio suites is unchanged and still passes.

Tests

Eight cases in tests/chat_test.py: the four mappings (auto, None, low, high) and four rejections (hgh, HIGH, medium, ""). The rejections fail on main. pytest -n 4: 877 passed; ruff check, ruff format --check, pyright clean. CHANGELOG entry under Unreleased / Changed; it will need a one-line merge against #232 or #233 if those land first, since each adds to the same section.

image(url, detail=...) mapped "low" and "high" and let anything else
fall through to DETAIL_AUTO, so image(url, detail="hgh") sent the image
at auto resolution with no signal to the caller. Every other literal in
the chat API (reasoning_effort, tool_choice, response_format, include,
agent_count, service_tier, search mode) and the image and video
converters raise ValueError on a value outside the Literal.

_image_detail_to_proto matches the three literals plus None, which keeps
meaning auto, and raises the same shaped ValueError as the rest. Four
new cases fail on main; 877 pass.
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