Skip to content
Merged
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
21 changes: 15 additions & 6 deletions coordinator/api/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"regexp"
"strconv"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 17 additions & 1 deletion coordinator/api/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 11 additions & 11 deletions coordinator/api/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 ───────────────────────────────────────────────────
Expand Down