Skip to content

Commit fe02e9a

Browse files
authored
Merge pull request #2224 from jarvis-u/main
fix: 错误解析responses api中的input字段
2 parents 84745d5 + d9b5748 commit fe02e9a

File tree

1 file changed

+65
-45
lines changed

1 file changed

+65
-45
lines changed

dto/openai_request.go

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,12 @@ type Reasoning struct {
897897
Summary string `json:"summary,omitempty"`
898898
}
899899

900+
type Input struct {
901+
Type string `json:"type,omitempty"`
902+
Role string `json:"role,omitempty"`
903+
Content json.RawMessage `json:"content,omitempty"`
904+
}
905+
900906
type MediaInput struct {
901907
Type string `json:"type"`
902908
Text string `json:"text,omitempty"`
@@ -915,7 +921,7 @@ func (r *OpenAIResponsesRequest) ParseInput() []MediaInput {
915921
return nil
916922
}
917923

918-
var inputs []MediaInput
924+
var mediaInputs []MediaInput
919925

920926
// Try string first
921927
// if str, ok := common.GetJsonType(r.Input); ok {
@@ -925,60 +931,74 @@ func (r *OpenAIResponsesRequest) ParseInput() []MediaInput {
925931
if common.GetJsonType(r.Input) == "string" {
926932
var str string
927933
_ = common.Unmarshal(r.Input, &str)
928-
inputs = append(inputs, MediaInput{Type: "input_text", Text: str})
929-
return inputs
934+
mediaInputs = append(mediaInputs, MediaInput{Type: "input_text", Text: str})
935+
return mediaInputs
930936
}
931937

932938
// Try array of parts
933939
if common.GetJsonType(r.Input) == "array" {
934-
var array []any
935-
_ = common.Unmarshal(r.Input, &array)
936-
for _, itemAny := range array {
937-
// Already parsed MediaInput
938-
if media, ok := itemAny.(MediaInput); ok {
939-
inputs = append(inputs, media)
940-
continue
941-
}
942-
// Generic map
943-
item, ok := itemAny.(map[string]any)
944-
if !ok {
945-
continue
946-
}
947-
typeVal, ok := item["type"].(string)
948-
if !ok {
949-
continue
940+
var inputs []Input
941+
_ = common.Unmarshal(r.Input, &inputs)
942+
for _, input := range inputs {
943+
if common.GetJsonType(input.Content) == "string" {
944+
var str string
945+
_ = common.Unmarshal(input.Content, &str)
946+
mediaInputs = append(mediaInputs, MediaInput{Type: "input_text", Text: str})
950947
}
951-
switch typeVal {
952-
case "input_text":
953-
text, _ := item["text"].(string)
954-
inputs = append(inputs, MediaInput{Type: "input_text", Text: text})
955-
case "input_image":
956-
// image_url may be string or object with url field
957-
var imageUrl string
958-
switch v := item["image_url"].(type) {
959-
case string:
960-
imageUrl = v
961-
case map[string]any:
962-
if url, ok := v["url"].(string); ok {
963-
imageUrl = url
948+
949+
if common.GetJsonType(input.Content) == "array" {
950+
var array []any
951+
_ = common.Unmarshal(input.Content, &array)
952+
for _, itemAny := range array {
953+
// Already parsed MediaContent
954+
if media, ok := itemAny.(MediaInput); ok {
955+
mediaInputs = append(mediaInputs, media)
956+
continue
964957
}
965-
}
966-
inputs = append(inputs, MediaInput{Type: "input_image", ImageUrl: imageUrl})
967-
case "input_file":
968-
// file_url may be string or object with url field
969-
var fileUrl string
970-
switch v := item["file_url"].(type) {
971-
case string:
972-
fileUrl = v
973-
case map[string]any:
974-
if url, ok := v["url"].(string); ok {
975-
fileUrl = url
958+
959+
// Generic map
960+
item, ok := itemAny.(map[string]any)
961+
if !ok {
962+
continue
963+
}
964+
965+
typeVal, ok := item["type"].(string)
966+
if !ok {
967+
continue
968+
}
969+
switch typeVal {
970+
case "input_text":
971+
text, _ := item["text"].(string)
972+
mediaInputs = append(mediaInputs, MediaInput{Type: "input_text", Text: text})
973+
case "input_image":
974+
// image_url may be string or object with url field
975+
var imageUrl string
976+
switch v := item["image_url"].(type) {
977+
case string:
978+
imageUrl = v
979+
case map[string]any:
980+
if url, ok := v["url"].(string); ok {
981+
imageUrl = url
982+
}
983+
}
984+
mediaInputs = append(mediaInputs, MediaInput{Type: "input_image", ImageUrl: imageUrl})
985+
case "input_file":
986+
// file_url may be string or object with url field
987+
var fileUrl string
988+
switch v := item["file_url"].(type) {
989+
case string:
990+
fileUrl = v
991+
case map[string]any:
992+
if url, ok := v["url"].(string); ok {
993+
fileUrl = url
994+
}
995+
}
996+
mediaInputs = append(mediaInputs, MediaInput{Type: "input_file", FileUrl: fileUrl})
976997
}
977998
}
978-
inputs = append(inputs, MediaInput{Type: "input_file", FileUrl: fileUrl})
979999
}
9801000
}
9811001
}
9821002

983-
return inputs
1003+
return mediaInputs
9841004
}

0 commit comments

Comments
 (0)