Skip to content

Commit c419264

Browse files
committed
fix(responses): handle empty and invalid rawJSON in ConvertOpenAIChatCompletionsResponseToOpenAIResponses
1 parent 6b23e2d commit c419264

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

internal/translator/openai/openai/responses/openai_openai-responses_response.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,20 @@ func ConvertOpenAIChatCompletionsResponseToOpenAIResponses(ctx context.Context,
6767
rawJSON = bytes.TrimSpace(rawJSON[5:])
6868
}
6969

70+
rawJSON = bytes.TrimSpace(rawJSON)
71+
if len(rawJSON) == 0 {
72+
return []string{}
73+
}
74+
if bytes.Equal(rawJSON, []byte("[DONE]")) {
75+
return []string{}
76+
}
77+
7078
root := gjson.ParseBytes(rawJSON)
71-
obj := root.Get("object").String()
72-
if obj != "chat.completion.chunk" {
79+
obj := root.Get("object")
80+
if obj.Exists() && obj.String() != "" && obj.String() != "chat.completion.chunk" {
81+
return []string{}
82+
}
83+
if !root.Get("choices").Exists() || !root.Get("choices").IsArray() {
7384
return []string{}
7485
}
7586

0 commit comments

Comments
 (0)