Skip to content

Support output_compression_quality in Gemini API #2346

@zhouyc98

Description

@zhouyc98

The ImageConfig currently includes fields like output_mime_type and output_compression_quality, but they are not supported in the Gemini API, as noted in the source code:

  output_mime_type: Optional[str] = Field(
      default=None,
      description="""MIME type of the generated image. This field is not
      supported in Gemini API.""",
  )
  output_compression_quality: Optional[int] = Field(
      default=None,
      description="""Compression quality of the generated image (for
      ``image/jpeg`` only). This field is not supported in Gemini API.""",
  )

See: https://github.com/googleapis/python-genai/blob/main/google/genai/types.py#L5431

Use Case

In production, we generate a large number of images via Gemini API.

Network bandwidth is a major cost factor.
Being able to control output compression would significantly reduce traffic and cost.

Observation

From testing with gemini-3.1-flash-image-preview, the output appears to be JPEG with very high quality (likely close to 100).

Example:

data = part.inline_data.data
mime_type = part.inline_data.mime_type
print(f'output inline_data: mime={mime_type}, bytes={len(data)}, MB={len(data)/1024/1024:.2f}')

Result:

output inline_data: mime=image/jpeg, bytes=3667955, MB=3.50

However, re-encoding the same image locally using:

image.save(..., format="JPEG", quality=92)

produces a file of only ~773 KB (~4× smaller) with no noticeable visual degradation.

Actually, the final image I delivered to end user is compressed with jpeg quality 92, and it is clear enough.

Problem

  • The API always returns high-quality images
  • Developers cannot control compression level
  • This leads to:
    • Significant network overhead
    • Higher proxy / egress cost
    • Unnecessary bandwidth usage at scale

Proposal

Support the following fields in ImageConfig:

  • output_mime_type (e.g. image/jpeg)
  • output_compression_quality (e.g. 80–95)

Even a simple implementation (JPEG-only, best-effort quality) would already be very valuable.

Additional Benefit

Reducing output size could also:

  • Improve latency (faster response transfer)
  • Reduce server/network load
  • Improve scalability of high-throughput applications

Metadata

Metadata

Labels

priority: p3Desirable enhancement or fix. May not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions