From 4d7f389b69776fa8ae754d2b0adc4c9167f63fd4 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sat, 10 Jan 2026 01:01:09 +0800 Subject: [PATCH] Fixed: #941 fix(translator): ensure fallback to valid originalRequestRawJSON in response handling --- .../claude_openai-responses_response.go | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/internal/translator/claude/openai/responses/claude_openai-responses_response.go b/internal/translator/claude/openai/responses/claude_openai-responses_response.go index 354be56e..593ec287 100644 --- a/internal/translator/claude/openai/responses/claude_openai-responses_response.go +++ b/internal/translator/claude/openai/responses/claude_openai-responses_response.go @@ -40,6 +40,16 @@ type claudeToResponsesState struct { var dataTag = []byte("data:") +func pickRequestJSON(originalRequestRawJSON, requestRawJSON []byte) []byte { + if len(originalRequestRawJSON) > 0 && gjson.ValidBytes(originalRequestRawJSON) { + return originalRequestRawJSON + } + if len(requestRawJSON) > 0 && gjson.ValidBytes(requestRawJSON) { + return requestRawJSON + } + return nil +} + func emitEvent(event string, payload string) string { return fmt.Sprintf("event: %s\ndata: %s", event, payload) } @@ -279,8 +289,9 @@ func ConvertClaudeResponseToOpenAIResponses(ctx context.Context, modelName strin completed, _ = sjson.Set(completed, "response.created_at", st.CreatedAt) // Inject original request fields into response as per docs/response.completed.json - if requestRawJSON != nil { - req := gjson.ParseBytes(requestRawJSON) + reqBytes := pickRequestJSON(originalRequestRawJSON, requestRawJSON) + if len(reqBytes) > 0 { + req := gjson.ParseBytes(reqBytes) if v := req.Get("instructions"); v.Exists() { completed, _ = sjson.Set(completed, "response.instructions", v.String()) } @@ -549,8 +560,9 @@ func ConvertClaudeResponseToOpenAIResponsesNonStream(_ context.Context, _ string out, _ = sjson.Set(out, "created_at", createdAt) // Inject request echo fields as top-level (similar to streaming variant) - if requestRawJSON != nil { - req := gjson.ParseBytes(requestRawJSON) + reqBytes := pickRequestJSON(originalRequestRawJSON, requestRawJSON) + if len(reqBytes) > 0 { + req := gjson.ParseBytes(reqBytes) if v := req.Get("instructions"); v.Exists() { out, _ = sjson.Set(out, "instructions", v.String()) }