Skip to content

Add image quality tier support for image generation models#108

Draft
adambalogh wants to merge 1 commit into
mainfrom
claude/hopeful-ride-21fe4z
Draft

Add image quality tier support for image generation models#108
adambalogh wants to merge 1 commit into
mainfrom
claude/hopeful-ride-21fe4z

Conversation

@adambalogh

Copy link
Copy Markdown
Contributor

Summary

Adds a quality request parameter (low | medium | high) to image generation endpoints, allowing callers to control output resolution across multiple providers. Each model maps the simple quality tier to its provider-specific size format via a new image_quality_sizes field in ModelConfig.

Key Changes

  • Model Registry (model_registry.py):

    • Added image_quality_sizes: Optional[dict[str, str]] field to ModelConfig to map quality tiers to provider-specific resolution strings
    • Configured quality mappings for ByteDance Seedream/Seedance (1K/2K/4K), Z.ai GLM-Image (1024x1024/1280x1280/2048x2048), and Gemini Nano Banana (1K/2K/4K)
    • Added VALID_IMAGE_QUALITIES constant and resolve_image_size() helper function to validate and map quality tiers to model-specific sizes
  • LLM Backend (llm_backend.py):

    • Updated get_chat_model_cached() to accept image_quality parameter and pass resolved image size to Gemini's image_config
    • Updated generate_images() to accept quality parameter and apply size overrides to provider payloads (Z.ai, Seedance, Seedream)
    • Models without resolution control (xAI Grok, Gemini 2.5 Flash Image) silently ignore the quality parameter
  • Chat Controller (chat_controller.py):

    • Added validation of quality field in CreateChatCompletionRequest with clean 400 error response for invalid values
    • Threaded quality parameter through both streaming and non-streaming image generation paths
    • Updated request serialization/deserialization to preserve quality across session tracking
  • API Models (create_chat_completion_request.py):

    • Added quality field to CreateChatCompletionRequest Pydantic model
  • OpenAPI Spec (openapi.yaml):

    • Documented quality parameter with enum values and provider-specific resolution mappings
  • Tests (test_image_generation.py):

    • Added comprehensive test coverage for quality tier mapping across providers
    • Tests verify correct size presets are sent to each provider's API
    • Tests confirm models without resolution control ignore the quality parameter
    • Tests validate error handling for invalid quality values
  • Documentation (CLAUDE.md):

    • Added section explaining image output resolution selection and per-model quality mappings

Implementation Details

  • Quality resolution happens at request time via resolve_image_size(), which validates the tier and looks up the model's provider-specific size string
  • The medium tier is configured to match each model's previous hardcoded default, ensuring backward compatibility when quality is omitted
  • Models without image_quality_sizes configured (e.g., xAI Grok) return None from resolve_image_size(), causing the provider default to be used
  • Validation occurs early in the request handler for clean error responses before any provider calls

https://claude.ai/code/session_01ENGmJDbJJHAM89HSGQuoeF

Expose a single `quality` request field (low/medium/high) that selects
output resolution for image-generation models. Each model maps the tier
to its own native size via `image_quality_sizes` in the registry:

- ByteDance Seedream / Seedance and Gemini image models: 1K / 2K / 4K
- Z.ai GLM-Image: 1024 / 1280 / 2048 px

Models with no resolution control (xAI Grok, Gemini 2.5 Flash Image)
ignore the option. The `medium` tier mirrors each model's previous
default, so omitting `quality` is unchanged. The field is folded into the
signed request hash, validated up front (clean 400 on bad values), and
documented in the OpenAPI spec.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ENGmJDbJJHAM89HSGQuoeF
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.

2 participants