Merge pull request #2224 from jarvis-u/main

fix: 错误解析responses api中的input字段
This commit is contained in:
Seefs
2025-11-22 00:31:24 +08:00
committed by GitHub

View File

@@ -897,6 +897,12 @@ type Reasoning struct {
Summary string `json:"summary,omitempty"` Summary string `json:"summary,omitempty"`
} }
type Input struct {
Type string `json:"type,omitempty"`
Role string `json:"role,omitempty"`
Content json.RawMessage `json:"content,omitempty"`
}
type MediaInput struct { type MediaInput struct {
Type string `json:"type"` Type string `json:"type"`
Text string `json:"text,omitempty"` Text string `json:"text,omitempty"`
@@ -915,7 +921,7 @@ func (r *OpenAIResponsesRequest) ParseInput() []MediaInput {
return nil return nil
} }
var inputs []MediaInput var mediaInputs []MediaInput
// Try string first // Try string first
// if str, ok := common.GetJsonType(r.Input); ok { // if str, ok := common.GetJsonType(r.Input); ok {
@@ -925,60 +931,74 @@ func (r *OpenAIResponsesRequest) ParseInput() []MediaInput {
if common.GetJsonType(r.Input) == "string" { if common.GetJsonType(r.Input) == "string" {
var str string var str string
_ = common.Unmarshal(r.Input, &str) _ = common.Unmarshal(r.Input, &str)
inputs = append(inputs, MediaInput{Type: "input_text", Text: str}) mediaInputs = append(mediaInputs, MediaInput{Type: "input_text", Text: str})
return inputs return mediaInputs
} }
// Try array of parts // Try array of parts
if common.GetJsonType(r.Input) == "array" { if common.GetJsonType(r.Input) == "array" {
var array []any var inputs []Input
_ = common.Unmarshal(r.Input, &array) _ = common.Unmarshal(r.Input, &inputs)
for _, itemAny := range array { for _, input := range inputs {
// Already parsed MediaInput if common.GetJsonType(input.Content) == "string" {
if media, ok := itemAny.(MediaInput); ok { var str string
inputs = append(inputs, media) _ = common.Unmarshal(input.Content, &str)
continue mediaInputs = append(mediaInputs, MediaInput{Type: "input_text", Text: str})
} }
// Generic map
item, ok := itemAny.(map[string]any) if common.GetJsonType(input.Content) == "array" {
if !ok { var array []any
continue _ = common.Unmarshal(input.Content, &array)
} for _, itemAny := range array {
typeVal, ok := item["type"].(string) // Already parsed MediaContent
if !ok { if media, ok := itemAny.(MediaInput); ok {
continue mediaInputs = append(mediaInputs, media)
} continue
switch typeVal { }
case "input_text":
text, _ := item["text"].(string) // Generic map
inputs = append(inputs, MediaInput{Type: "input_text", Text: text}) item, ok := itemAny.(map[string]any)
case "input_image": if !ok {
// image_url may be string or object with url field continue
var imageUrl string }
switch v := item["image_url"].(type) {
case string: typeVal, ok := item["type"].(string)
imageUrl = v if !ok {
case map[string]any: continue
if url, ok := v["url"].(string); ok { }
imageUrl = url switch typeVal {
case "input_text":
text, _ := item["text"].(string)
mediaInputs = append(mediaInputs, MediaInput{Type: "input_text", Text: text})
case "input_image":
// image_url may be string or object with url field
var imageUrl string
switch v := item["image_url"].(type) {
case string:
imageUrl = v
case map[string]any:
if url, ok := v["url"].(string); ok {
imageUrl = url
}
}
mediaInputs = append(mediaInputs, MediaInput{Type: "input_image", ImageUrl: imageUrl})
case "input_file":
// file_url may be string or object with url field
var fileUrl string
switch v := item["file_url"].(type) {
case string:
fileUrl = v
case map[string]any:
if url, ok := v["url"].(string); ok {
fileUrl = url
}
}
mediaInputs = append(mediaInputs, MediaInput{Type: "input_file", FileUrl: fileUrl})
} }
} }
inputs = append(inputs, MediaInput{Type: "input_image", ImageUrl: imageUrl})
case "input_file":
// file_url may be string or object with url field
var fileUrl string
switch v := item["file_url"].(type) {
case string:
fileUrl = v
case map[string]any:
if url, ok := v["url"].(string); ok {
fileUrl = url
}
}
inputs = append(inputs, MediaInput{Type: "input_file", FileUrl: fileUrl})
} }
} }
} }
return inputs return mediaInputs
} }