Skip to content

Commit d9b5748

Browse files
committed
fix: 错误解析responses api中的input字段
1 parent 974df5e commit d9b5748

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
@@ -895,6 +895,12 @@ type Reasoning struct {
895895
Summary string `json:"summary,omitempty"`
896896
}
897897

898+
type Input struct {
899+
Type string `json:"type,omitempty"`
900+
Role string `json:"role,omitempty"`
901+
Content json.RawMessage `json:"content,omitempty"`
902+
}
903+
898904
type MediaInput struct {
899905
Type string `json:"type"`
900906
Text string `json:"text,omitempty"`
@@ -913,7 +919,7 @@ func (r *OpenAIResponsesRequest) ParseInput() []MediaInput {
913919
return nil
914920
}
915921

916-
var inputs []MediaInput
922+
var mediaInputs []MediaInput
917923

918924
// Try string first
919925
// if str, ok := common.GetJsonType(r.Input); ok {
@@ -923,60 +929,74 @@ func (r *OpenAIResponsesRequest) ParseInput() []MediaInput {
923929
if common.GetJsonType(r.Input) == "string" {
924930
var str string
925931
_ = common.Unmarshal(r.Input, &str)
926-
inputs = append(inputs, MediaInput{Type: "input_text", Text: str})
927-
return inputs
932+
mediaInputs = append(mediaInputs, MediaInput{Type: "input_text", Text: str})
933+
return mediaInputs
928934
}
929935

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

981-
return inputs
1001+
return mediaInputs
9821002
}

0 commit comments

Comments
 (0)