Repository navigation
Expand file tree
/
Copy pathimagegenerationrequest.py
More file actions
209 lines (174 loc) · 8.98 KB
/
Copy pathimagegenerationrequest.py
File metadata and controls
209 lines (174 loc) · 8.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
from __future__ import annotations
from .contentpartimage import ContentPartImage, ContentPartImageTypedDict
from .imagegenerationproviderpreferences import (
ImageGenerationProviderPreferences,
ImageGenerationProviderPreferencesTypedDict,
)
from openrouter.types import BaseModel, UNSET_SENTINEL, UnrecognizedStr
from pydantic import model_serializer
from typing import List, Literal, Optional, Union
from typing_extensions import NotRequired, TypedDict
ImageGenerationRequestAspectRatio = Union[
Literal[
"1:1",
"1:2",
"1:4",
"1:8",
"2:1",
"2:3",
"2.35:1",
"3:2",
"3:4",
"4:1",
"4:3",
"4:5",
"5:2",
"5:4",
"8:1",
"9:16",
"16:9",
"9:19.5",
"19.5:9",
"9:20",
"20:9",
"9:21",
"21:9",
"auto",
],
UnrecognizedStr,
]
r"""Normalized aspect ratio of the generated image. Providers clamp to their supported subset."""
ImageGenerationRequestBackground = Union[
Literal[
"auto",
"transparent",
"opaque",
],
UnrecognizedStr,
]
r"""Background treatment. `transparent` requires an output_format that supports alpha (png or webp)."""
ImageGenerationRequestOutputFormat = Union[
Literal[
"png",
"jpeg",
"webp",
"svg",
],
UnrecognizedStr,
]
r"""Encoding of the returned image bytes. Most models produce raster formats (png, jpeg, webp). SVG is supported by vectorization models (e.g. Quiver) — the SVG markup is UTF-8 base64-encoded in `b64_json`."""
ImageGenerationRequestQuality = Union[
Literal[
"auto",
"low",
"medium",
"high",
"xhigh",
"max",
],
UnrecognizedStr,
]
r"""Rendering quality. Providers without a quality knob ignore this."""
ImageGenerationRequestResolution = Union[
Literal[
"512",
"1K",
"2K",
"4K",
],
UnrecognizedStr,
]
r"""Normalized resolution tier of the generated image. Concrete pixel dimensions are derived per-provider."""
class ImageGenerationRequestTypedDict(TypedDict):
r"""Image generation request input"""
model: str
r"""The image generation model to use"""
prompt: str
r"""Text description of the desired image"""
aspect_ratio: NotRequired[ImageGenerationRequestAspectRatio]
r"""Normalized aspect ratio of the generated image. Providers clamp to their supported subset."""
background: NotRequired[ImageGenerationRequestBackground]
r"""Background treatment. `transparent` requires an output_format that supports alpha (png or webp)."""
input_references: NotRequired[List[ContentPartImageTypedDict]]
r"""Reference images to guide image-to-image generation, as base64 data URLs or HTTP(S) URLs."""
n: NotRequired[int]
r"""Upper bound on the number of images to generate (1-10). Providers may return fewer images, and providers that only support single-image generation reject n > 1."""
output_compression: NotRequired[int]
r"""Compression level (0-100) for webp/jpeg output. Ignored for png and by providers without a compression knob."""
output_format: NotRequired[ImageGenerationRequestOutputFormat]
r"""Encoding of the returned image bytes. Most models produce raster formats (png, jpeg, webp). SVG is supported by vectorization models (e.g. Quiver) — the SVG markup is UTF-8 base64-encoded in `b64_json`."""
provider: NotRequired[ImageGenerationProviderPreferencesTypedDict]
r"""Provider routing preferences and provider-specific passthrough configuration."""
quality: NotRequired[ImageGenerationRequestQuality]
r"""Rendering quality. Providers without a quality knob ignore this."""
resolution: NotRequired[ImageGenerationRequestResolution]
r"""Normalized resolution tier of the generated image. Concrete pixel dimensions are derived per-provider."""
seed: NotRequired[int]
r"""If specified, the generation will sample deterministically, such that repeated requests with the same seed and parameters should return the same result. Determinism is not guaranteed for all providers."""
size: NotRequired[str]
r"""Optional. A convenience shorthand for output dimensions — pass a tier (\"2K\", \"4K\") or explicit pixels (\"2048x2048\") and we normalize it to the right dimensions for the chosen provider. A tier size is equivalent to setting `resolution` and combines with `aspect_ratio`. An explicit pixel size is authoritative: a mismatched `resolution` or `aspect_ratio` alongside it is rejected with a 400."""
stream: NotRequired[bool]
r"""If true, partial images are streamed as SSE events as they become available. Only supported by providers with native streaming (currently OpenAI). Non-streaming providers ignore this flag and return a buffered response."""
user: NotRequired[str]
r"""A stable identifier for your end-users. Used to help detect and prevent abuse. Never sent to providers verbatim: for providers whose data policy requires user IDs, it is folded into a hashed, per-account upstream user identifier."""
class ImageGenerationRequest(BaseModel):
r"""Image generation request input"""
model: str
r"""The image generation model to use"""
prompt: str
r"""Text description of the desired image"""
aspect_ratio: Optional[ImageGenerationRequestAspectRatio] = None
r"""Normalized aspect ratio of the generated image. Providers clamp to their supported subset."""
background: Optional[ImageGenerationRequestBackground] = None
r"""Background treatment. `transparent` requires an output_format that supports alpha (png or webp)."""
input_references: Optional[List[ContentPartImage]] = None
r"""Reference images to guide image-to-image generation, as base64 data URLs or HTTP(S) URLs."""
n: Optional[int] = None
r"""Upper bound on the number of images to generate (1-10). Providers may return fewer images, and providers that only support single-image generation reject n > 1."""
output_compression: Optional[int] = None
r"""Compression level (0-100) for webp/jpeg output. Ignored for png and by providers without a compression knob."""
output_format: Optional[ImageGenerationRequestOutputFormat] = None
r"""Encoding of the returned image bytes. Most models produce raster formats (png, jpeg, webp). SVG is supported by vectorization models (e.g. Quiver) — the SVG markup is UTF-8 base64-encoded in `b64_json`."""
provider: Optional[ImageGenerationProviderPreferences] = None
r"""Provider routing preferences and provider-specific passthrough configuration."""
quality: Optional[ImageGenerationRequestQuality] = None
r"""Rendering quality. Providers without a quality knob ignore this."""
resolution: Optional[ImageGenerationRequestResolution] = None
r"""Normalized resolution tier of the generated image. Concrete pixel dimensions are derived per-provider."""
seed: Optional[int] = None
r"""If specified, the generation will sample deterministically, such that repeated requests with the same seed and parameters should return the same result. Determinism is not guaranteed for all providers."""
size: Optional[str] = None
r"""Optional. A convenience shorthand for output dimensions — pass a tier (\"2K\", \"4K\") or explicit pixels (\"2048x2048\") and we normalize it to the right dimensions for the chosen provider. A tier size is equivalent to setting `resolution` and combines with `aspect_ratio`. An explicit pixel size is authoritative: a mismatched `resolution` or `aspect_ratio` alongside it is rejected with a 400."""
stream: Optional[bool] = None
r"""If true, partial images are streamed as SSE events as they become available. Only supported by providers with native streaming (currently OpenAI). Non-streaming providers ignore this flag and return a buffered response."""
user: Optional[str] = None
r"""A stable identifier for your end-users. Used to help detect and prevent abuse. Never sent to providers verbatim: for providers whose data policy requires user IDs, it is folded into a hashed, per-account upstream user identifier."""
@model_serializer(mode="wrap")
def serialize_model(self, handler):
optional_fields = set(
[
"aspect_ratio",
"background",
"input_references",
"n",
"output_compression",
"output_format",
"provider",
"quality",
"resolution",
"seed",
"size",
"stream",
"user",
]
)
serialized = handler(self)
m = {}
for n, f in type(self).model_fields.items():
k = f.alias or n
val = serialized.get(k, serialized.get(n))
if val != UNSET_SENTINEL:
if val is not None or k not in optional_fields:
m[k] = val
return m