mirror of
https://github.com/mudler/LocalAI.git
synced 2026-04-04 15:04:27 -04:00
fix: Usage for image generation is incorrect (and causes error in LiteLLM) (#7786)
* fix: Add usage fields to image generation response for OpenAI API compatibility Fixes #7354 Added input_tokens, output_tokens, and input_tokens_details fields to the image generation API response to comply with OpenAI's image generation API specification. This resolves validation errors in LiteLLM and the OpenAI SDK. Changes: - Added InputTokensDetails struct with text_tokens and image_tokens fields - Extended OpenAIUsage struct with input_tokens, output_tokens, and input_tokens_details - Updated ImageEndpoint to populate usage object with required fields - Updated InpaintingEndpoint to populate usage object with required fields - All fields initialized to 0 as per current behavior 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> Signed-off-by: majiayu000 <1835304752@qq.com> * fix: Correct usage field types for image generation API compatibility Changed InputTokens and OutputTokens from pointer types (*int) to regular int types to match OpenAI API specification. This fixes validation errors with LiteLLM and OpenAI SDK when parsing image generation responses. Fixes #7354 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> Signed-off-by: majiayu000 <1835304752@qq.com> --------- Signed-off-by: majiayu000 <1835304752@qq.com> Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -232,6 +232,17 @@ func ImageEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfi
|
||||
ID: id,
|
||||
Created: created,
|
||||
Data: result,
|
||||
Usage: schema.OpenAIUsage{
|
||||
PromptTokens: 0,
|
||||
CompletionTokens: 0,
|
||||
TotalTokens: 0,
|
||||
InputTokens: 0,
|
||||
OutputTokens: 0,
|
||||
InputTokensDetails: &schema.InputTokensDetails{
|
||||
TextTokens: 0,
|
||||
ImageTokens: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
jsonResult, _ := json.Marshal(resp)
|
||||
|
||||
@@ -258,6 +258,17 @@ func InpaintingEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, app
|
||||
Data: []schema.Item{{
|
||||
URL: imgPath,
|
||||
}},
|
||||
Usage: schema.OpenAIUsage{
|
||||
PromptTokens: 0,
|
||||
CompletionTokens: 0,
|
||||
TotalTokens: 0,
|
||||
InputTokens: 0,
|
||||
OutputTokens: 0,
|
||||
InputTokensDetails: &schema.InputTokensDetails{
|
||||
TextTokens: 0,
|
||||
ImageTokens: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// mark success so defer cleanup will not remove output files
|
||||
|
||||
@@ -18,10 +18,19 @@ type ErrorResponse struct {
|
||||
Error *APIError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type InputTokensDetails struct {
|
||||
TextTokens int `json:"text_tokens"`
|
||||
ImageTokens int `json:"image_tokens"`
|
||||
}
|
||||
|
||||
type OpenAIUsage struct {
|
||||
PromptTokens int `json:"prompt_tokens"`
|
||||
CompletionTokens int `json:"completion_tokens"`
|
||||
TotalTokens int `json:"total_tokens"`
|
||||
// Fields for image generation API compatibility
|
||||
InputTokens int `json:"input_tokens,omitempty"`
|
||||
OutputTokens int `json:"output_tokens,omitempty"`
|
||||
InputTokensDetails *InputTokensDetails `json:"input_tokens_details,omitempty"`
|
||||
// Extra timing data, disabled by default as is't not a part of OpenAI specification
|
||||
TimingPromptProcessing float64 `json:"timing_prompt_processing,omitempty"`
|
||||
TimingTokenGeneration float64 `json:"timing_token_generation,omitempty"`
|
||||
@@ -49,11 +58,11 @@ type OpenAIResponse struct {
|
||||
}
|
||||
|
||||
type Choice struct {
|
||||
Index int `json:"index"`
|
||||
FinishReason *string `json:"finish_reason"`
|
||||
Message *Message `json:"message,omitempty"`
|
||||
Delta *Message `json:"delta,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
Index int `json:"index"`
|
||||
FinishReason *string `json:"finish_reason"`
|
||||
Message *Message `json:"message,omitempty"`
|
||||
Delta *Message `json:"delta,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
Logprobs *Logprobs `json:"logprobs,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user