diff --git a/coordinator/api/consumer.go b/coordinator/api/consumer.go index df6fdeb5df..8c833de172 100644 --- a/coordinator/api/consumer.go +++ b/coordinator/api/consumer.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "io" + "log" "net/http" "regexp" "strconv" @@ -2239,7 +2240,6 @@ func writeResponsesStreamOutput(w http.ResponseWriter, flusher http.Flusher, pr // handleNonStreamingResponseWithFirstChunk collects all chunks from the // provider and assembles them into a single OpenAI-compatible JSON response. - // If firstChunk is non-empty, it is prepended to the collected chunks. func (s *Server) handleNonStreamingResponseWithFirstChunk(w http.ResponseWriter, r *http.Request, pr *registry.PendingRequest, firstChunk string) { ctx, cancel := context.WithTimeout(r.Context(), inferenceTimeout) @@ -2296,8 +2296,17 @@ func (s *Server) handleNonStreamingResponseWithFirstChunk(w http.ResponseWriter, normalizeCompleteChatResponse(obj, pr.Model) if pr.IsResponsesAPI { var chatResp types.ChatCompletionResponse - b, _ := json.Marshal(obj) - json.Unmarshal(b, &chatResp) + b, err := json.Marshal(obj) + if err != nil { + log.Printf("WARN: failed to marshal chat response for Responses API conversion: %v", err) + writeJSON(w, http.StatusBadGateway, errorResponse("provider_error", "invalid provider response")) + return + } + if err := json.Unmarshal(b, &chatResp); err != nil { + log.Printf("WARN: failed to unmarshal chat response into typed struct: %v", err) + writeJSON(w, http.StatusBadGateway, errorResponse("provider_error", "invalid provider response")) + return + } respObj := chatCompletionToResponses(chatResp, pr.Model, pr.SESignature, pr.ResponseHash) writeJSON(w, http.StatusOK, respObj) return @@ -2676,12 +2685,12 @@ func buildResponsesUsage(promptTokens, completionTokens uint64, reasoningTokens } } -func buildResponsesIncompleteDetails(finishReason string) any { +func buildResponsesIncompleteDetails(finishReason string) *types.ResponsesIncompleteDetail { switch finishReason { case "length": - return types.ResponsesIncompleteDetail{Reason: "max_output_tokens"} + return &types.ResponsesIncompleteDetail{Reason: "max_output_tokens"} case "content_filter": - return types.ResponsesIncompleteDetail{Reason: "content_filter"} + return &types.ResponsesIncompleteDetail{Reason: "content_filter"} default: return nil } diff --git a/coordinator/api/consumer_test.go b/coordinator/api/consumer_test.go index ee47f40a53..ffbe6ac0a3 100644 --- a/coordinator/api/consumer_test.go +++ b/coordinator/api/consumer_test.go @@ -1066,10 +1066,26 @@ func TestChatCompletionToResponses(t *testing.T) { if call["type"] != "function_call" || call["call_id"] != "call_123" { t.Fatalf("function call output = %#v", call) } - usage := got.Usage.(types.ResponsesUsage) + usage := got.Usage if usage.InputTokens != 10 || usage.OutputTokens != 5 { t.Fatalf("usage = %#v", usage) } + + // Verify wire format preserves zero-valued fields. + b, err := json.Marshal(got) + if err != nil { + t.Fatalf("marshal: %v", err) + } + wire := string(b) + if !strings.Contains(wire, `"incomplete_details"`) { + t.Errorf("wire output missing incomplete_details field: %s", wire) + } + if !strings.Contains(wire, `"cached_tokens"`) { + t.Errorf("wire output missing cached_tokens in usage details: %s", wire) + } + if !strings.Contains(wire, `"reasoning_tokens"`) { + t.Errorf("wire output missing reasoning_tokens in usage details: %s", wire) + } } func TestExtractMessageWithNullFields(t *testing.T) { diff --git a/coordinator/api/types/types.go b/coordinator/api/types/types.go index 8917a03e4e..9d36600452 100644 --- a/coordinator/api/types/types.go +++ b/coordinator/api/types/types.go @@ -51,8 +51,8 @@ type ChatCompletionResponse struct { // ResponsesUsageDetail holds token breakdown details. type ResponsesUsageDetail struct { - CachedTokens int `json:"cached_tokens,omitempty"` - ReasoningTokens int `json:"reasoning_tokens,omitempty"` + CachedTokens int `json:"cached_tokens"` + ReasoningTokens int `json:"reasoning_tokens"` } // ResponsesUsage is the usage object in a Responses API response. @@ -70,15 +70,15 @@ type ResponsesIncompleteDetail struct { // ResponsesResponse is an OpenAI-compatible Responses API response. type ResponsesResponse struct { - ID string `json:"id"` - Object string `json:"object"` - CreatedAt int64 `json:"created_at"` - Model string `json:"model"` - Output []any `json:"output"` - Usage any `json:"usage"` - IncompleteDetail any `json:"incomplete_details,omitempty"` - SESignature string `json:"se_signature,omitempty"` - ResponseHash string `json:"response_hash,omitempty"` + ID string `json:"id"` + Object string `json:"object"` + CreatedAt int64 `json:"created_at"` + Model string `json:"model"` + Output []any `json:"output"` + Usage ResponsesUsage `json:"usage"` + IncompleteDetail *ResponsesIncompleteDetail `json:"incomplete_details"` + SESignature string `json:"se_signature,omitempty"` + ResponseHash string `json:"response_hash,omitempty"` } // ── GET /v1/models ───────────────────────────────────────────────────