Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions internal/server/handlers_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ func responsesContentText(content interface{}) string {
func buildResponsesToolResponse(id, modelID string, created int64, resp api.OpenAIChatResponse, inputTokens int, status string) map[string]interface{} {
output, outputText := responsesOutputFromOpenAIChatResponse(resp)
usage := responsesUsageFromOpenAI(resp, inputTokens, outputText)
return map[string]interface{}{
response := map[string]interface{}{
"id": id,
"object": "response",
"created_at": created,
Expand All @@ -831,6 +831,23 @@ func buildResponsesToolResponse(id, modelID string, created int64, resp api.Open
"parallel_tool_calls": responsesParallelToolCalls(output),
"usage": usage,
}
markResponsesIncomplete(response, resp)
return response
}

// markResponsesIncomplete reports a reply the backend cut off (token limit or
// content filter) as incomplete instead of completed.
func markResponsesIncomplete(response map[string]interface{}, resp api.OpenAIChatResponse) bool {
if len(resp.Choices) == 0 || !isCutOffFinishReason(resp.Choices[0].FinishReason) {
return false
}
reason := *resp.Choices[0].FinishReason
if reason == "length" {
reason = "max_output_tokens"
}
response["status"] = "incomplete"
response["incomplete_details"] = map[string]interface{}{"reason": reason}
return true
}

func responsesOutputFromOpenAIChatResponse(resp api.OpenAIChatResponse) ([]interface{}, string) {
Expand Down Expand Up @@ -1078,20 +1095,25 @@ func writeResponsesToolStream(w http.ResponseWriter, id, modelID string, created
}
}

writeResponsesSSE(w, "response.completed", map[string]interface{}{
"type": "response.completed",
response := map[string]interface{}{
"id": id,
"object": "response",
"created_at": created,
"status": "completed",
"model": modelID,
"output": output,
"output_text": outputText,
"parallel_tool_calls": responsesParallelToolCalls(output),
"usage": usage,
}
event := "response.completed"
if markResponsesIncomplete(response, resp) {
event = "response.incomplete"
}
writeResponsesSSE(w, event, map[string]interface{}{
"type": event,
"sequence_number": nextSequence(),
"response": map[string]interface{}{
"id": id,
"object": "response",
"created_at": created,
"status": "completed",
"model": modelID,
"output": output,
"output_text": outputText,
"parallel_tool_calls": responsesParallelToolCalls(output),
"usage": usage,
},
"response": response,
})
}

Expand Down
52 changes: 52 additions & 0 deletions internal/server/handlers_responses_incomplete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package server

import (
"net/http/httptest"
"strings"
"testing"

"github.com/opencsgs/csglite/pkg/api"
)

func TestResponsesReportsCutOffReplyAsIncomplete(t *testing.T) {
for finishReason, wantReason := range map[string]string{
"length": "max_output_tokens",
"content_filter": "content_filter",
"stop": "",
"tool_calls": "",
} {
t.Run(finishReason, func(t *testing.T) {
reason := finishReason
resp := api.OpenAIChatResponse{
Choices: []api.OpenAIChoice{{
Message: &api.Message{Role: "assistant", Content: "partial answer"},
FinishReason: &reason,
}},
}
wantStatus, wantEvent := "completed", "response.completed"
if wantReason != "" {
wantStatus, wantEvent = "incomplete", "response.incomplete"
}

body := buildResponsesToolResponse("resp_1", "m", 1, resp, 3, "completed")
if body["status"] != wantStatus {
t.Fatalf("status = %v, want %s", body["status"], wantStatus)
}
details, _ := body["incomplete_details"].(map[string]interface{})
if got := stringValue(details["reason"]); got != wantReason {
t.Fatalf("incomplete_details.reason = %q, want %q", got, wantReason)
}

rec := httptest.NewRecorder()
writeResponsesToolStream(rec, "resp_1", "m", 1, resp, 3)
events := strings.Split(strings.TrimSpace(rec.Body.String()), "\n\n")
last := events[len(events)-1]
if !strings.HasPrefix(last, "event: "+wantEvent+"\n") || !strings.Contains(last, `"type":"`+wantEvent+`"`) {
t.Fatalf("last event = %s, want %s", last, wantEvent)
}
if wantReason != "" && !strings.Contains(last, `"incomplete_details":{"reason":"`+wantReason+`"}`) {
t.Fatalf("last event = %s, want incomplete_details", last)
}
})
}
}
Loading