diff --git a/internal/translator/openai/claude/openai_claude_request.go b/internal/translator/openai/claude/openai_claude_request.go index 769c2275..42f2783c 100644 --- a/internal/translator/openai/claude/openai_claude_request.go +++ b/internal/translator/openai/claude/openai_claude_request.go @@ -60,11 +60,7 @@ func convertClaudeRequestToOpenAI(modelName string, inputRawJSON []byte, stream return true }) if len(stops) > 0 { - if len(stops) == 1 { - out, _ = sjson.SetBytes(out, "stop", stops[0]) - } else { - out, _ = sjson.SetBytes(out, "stop", stops) - } + out, _ = sjson.SetBytes(out, "stop", stops) } } } diff --git a/internal/translator/openai/claude/openai_claude_request_test.go b/internal/translator/openai/claude/openai_claude_request_test.go index 40b33eaf..4b698bf8 100644 --- a/internal/translator/openai/claude/openai_claude_request_test.go +++ b/internal/translator/openai/claude/openai_claude_request_test.go @@ -869,3 +869,52 @@ func TestConvertClaudeRequestToOpenAI_StripsClaudeCodeAttribution(t *testing.T) t.Fatalf("Unexpected system content: %q", got) } } + +func TestConvertClaudeRequestToOpenAI_StopSequences(t *testing.T) { + tests := []struct { + name string + inputJSON string + wantStop []string + }{ + { + name: "single stop sequence is emitted as array", + inputJSON: `{ + "model": "claude-3-opus", + "stop_sequences": [""], + "messages": [{"role": "user", "content": "hi"}] + }`, + wantStop: []string{""}, + }, + { + name: "multiple stop sequences are emitted as array", + inputJSON: `{ + "model": "claude-3-opus", + "stop_sequences": ["stop1", "stop2"], + "messages": [{"role": "user", "content": "hi"}] + }`, + wantStop: []string{"stop1", "stop2"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + output := ConvertClaudeRequestToOpenAI("gpt-4o", []byte(tt.inputJSON), false) + stopRes := gjson.GetBytes(output, "stop") + if !stopRes.Exists() { + t.Fatalf("expected 'stop' field in output, got: %s", string(output)) + } + if !stopRes.IsArray() { + t.Fatalf("expected 'stop' field to be JSON array, got: %s", stopRes.Raw) + } + items := stopRes.Array() + if len(items) != len(tt.wantStop) { + t.Fatalf("expected %d stop items, got %d (%v)", len(tt.wantStop), len(items), stopRes.Raw) + } + for i, want := range tt.wantStop { + if items[i].String() != want { + t.Errorf("stop[%d] = %q, want %q", i, items[i].String(), want) + } + } + }) + } +}