Skip to content

Commit 0429b4d

Browse files
nirgaclaude
andcommitted
fix: restore support for gpt-image-1 low quality parameter
- Add back support for low/medium/high quality values for gpt-image-1 model - Update token cost documentation to reflect both gpt-image-1 and DALL-E 3 - Fix default quality logic based on model (gpt-image-1 defaults to 'high') - Support all valid quality parameters: low, medium, high, standard, hd The low quality option is valid for gpt-image-1 and provides faster, lower-cost image generation. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e999c45 commit 0429b4d

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

packages/instrumentation-openai/src/image-wrappers.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,50 @@ import type {
1212
* Calculate completion tokens for image generation based on OpenAI's actual token costs
1313
*
1414
* Token costs based on OpenAI documentation:
15-
* Quality Square (1024×1024) Portrait (1024×1536) Landscape (1536×1024)
16-
* Low 272 tokens 408 tokens 400 tokens
17-
* Medium 1056 tokens 1584 tokens 1568 tokens
18-
* High 4160 tokens 6240 tokens 6208 tokens
15+
* For gpt-image-1: Square (1024×1024) Portrait (1024×1536) Landscape (1536×1024)
16+
* Low 272 tokens 408 tokens 400 tokens
17+
* Medium 1056 tokens 1584 tokens 1568 tokens
18+
* High 4160 tokens 6240 tokens 6208 tokens
19+
*
20+
* For DALL-E 3:
21+
* Standard 1056 tokens 1584 tokens 1568 tokens
22+
* HD 4160 tokens 6240 tokens 6208 tokens
1923
*/
2024
function calculateImageGenerationTokens(params: any, imageCount: number): number {
2125
const size = params?.size || "1024x1024";
22-
const quality = params?.quality || "standard"; // OpenAI defaults to "standard" which maps to "medium"
26+
const quality = params?.quality || (params?.model === "gpt-image-1" ? "high" : "standard");
2327

24-
// Map quality to token costs
28+
// Map quality to token costs (supports both gpt-image-1 and DALL-E 3 formats)
2529
const tokenCosts: Record<string, Record<string, number>> = {
26-
"standard": { // Maps to "medium" quality
30+
// gpt-image-1 quality values
31+
"low": {
32+
"1024x1024": 272,
33+
"1024x1536": 408,
34+
"1536x1024": 400,
35+
},
36+
"medium": {
37+
"1024x1024": 1056,
38+
"1024x1536": 1584,
39+
"1536x1024": 1568,
40+
},
41+
"high": {
42+
"1024x1024": 4160,
43+
"1024x1536": 6240,
44+
"1536x1024": 6208,
45+
},
46+
// DALL-E 3 quality values (same costs as medium/high above)
47+
"standard": {
2748
"1024x1024": 1056,
2849
"1024x1536": 1584,
2950
"1536x1024": 1568,
3051
},
31-
"hd": { // Maps to "high" quality
52+
"hd": {
3253
"1024x1024": 4160,
3354
"1024x1536": 6240,
3455
"1536x1024": 6208,
3556
}
3657
};
3758

38-
// For low quality (not supported by OpenAI API directly, but included for completeness)
39-
if (quality === "low") {
40-
const lowQualityCosts: Record<string, number> = {
41-
"1024x1024": 272,
42-
"1024x1536": 408,
43-
"1536x1024": 400,
44-
};
45-
return (lowQualityCosts[size] || 272) * imageCount;
46-
}
4759

4860
// Get tokens per image for the given quality and size
4961
const tokensPerImage = tokenCosts[quality]?.[size] || tokenCosts["standard"]["1024x1024"];

0 commit comments

Comments
 (0)