|
| 1 | +package openai_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/sashabaranov/go-openai" |
| 8 | +) |
| 9 | + |
| 10 | +// TestChatCompletionStreamChoiceDelta_ReasoningFieldSupport tests that both |
| 11 | +// reasoning_content and reasoning fields are properly supported in streaming responses. |
| 12 | +func TestChatCompletionStreamChoiceDelta_ReasoningFieldSupport(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + jsonData string |
| 16 | + expected string |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "DeepSeek style - reasoning_content", |
| 20 | + jsonData: `{"role":"assistant","content":"Hello","reasoning_content":"This is my reasoning"}`, |
| 21 | + expected: "This is my reasoning", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "OpenAI style - reasoning", |
| 25 | + jsonData: `{"role":"assistant","content":"Hello","reasoning":"This is my reasoning"}`, |
| 26 | + expected: "This is my reasoning", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "Both fields present - reasoning_content takes priority", |
| 30 | + jsonData: `{"role":"assistant","content":"Hello","reasoning_content":"Priority reasoning","reasoning":"Fallback reasoning"}`, |
| 31 | + expected: "Priority reasoning", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "Only reasoning field", |
| 35 | + jsonData: `{"role":"assistant","reasoning":"Only reasoning field"}`, |
| 36 | + expected: "Only reasoning field", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "No reasoning fields", |
| 40 | + jsonData: `{"role":"assistant","content":"Hello"}`, |
| 41 | + expected: "", |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, tt := range tests { |
| 46 | + t.Run(tt.name, func(t *testing.T) { |
| 47 | + var delta openai.ChatCompletionStreamChoiceDelta |
| 48 | + err := json.Unmarshal([]byte(tt.jsonData), &delta) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("Failed to unmarshal JSON: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + if delta.ReasoningContent != tt.expected { |
| 54 | + t.Errorf("Expected ReasoningContent to be %q, got %q", tt.expected, delta.ReasoningContent) |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// TestChatCompletionMessage_ReasoningFieldSupport tests that both |
| 61 | +// reasoning_content and reasoning fields are properly supported in chat completion messages. |
| 62 | +func TestChatCompletionMessage_ReasoningFieldSupport(t *testing.T) { |
| 63 | + tests := []struct { |
| 64 | + name string |
| 65 | + jsonData string |
| 66 | + expected string |
| 67 | + }{ |
| 68 | + { |
| 69 | + name: "DeepSeek style - reasoning_content", |
| 70 | + jsonData: `{"role":"assistant","content":"Hello","reasoning_content":"This is my reasoning"}`, |
| 71 | + expected: "This is my reasoning", |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "OpenAI style - reasoning", |
| 75 | + jsonData: `{"role":"assistant","content":"Hello","reasoning":"This is my reasoning"}`, |
| 76 | + expected: "This is my reasoning", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "Both fields present - reasoning_content takes priority", |
| 80 | + jsonData: `{"role":"assistant","content":"Hello","reasoning_content":"Priority reasoning","reasoning":"Fallback reasoning"}`, |
| 81 | + expected: "Priority reasoning", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "Only reasoning field", |
| 85 | + jsonData: `{"role":"assistant","reasoning":"Only reasoning field"}`, |
| 86 | + expected: "Only reasoning field", |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "No reasoning fields", |
| 90 | + jsonData: `{"role":"assistant","content":"Hello"}`, |
| 91 | + expected: "", |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + for _, tt := range tests { |
| 96 | + t.Run(tt.name, func(t *testing.T) { |
| 97 | + var msg openai.ChatCompletionMessage |
| 98 | + err := json.Unmarshal([]byte(tt.jsonData), &msg) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("Failed to unmarshal JSON: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + if msg.ReasoningContent != tt.expected { |
| 104 | + t.Errorf("Expected ReasoningContent to be %q, got %q", tt.expected, msg.ReasoningContent) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// TestChatCompletionMessage_MultiContent_ReasoningFieldSupport tests reasoning field support |
| 111 | +// with MultiContent messages. |
| 112 | +func TestChatCompletionMessage_MultiContent_ReasoningFieldSupport(t *testing.T) { |
| 113 | + tests := []struct { |
| 114 | + name string |
| 115 | + jsonData string |
| 116 | + expected string |
| 117 | + }{ |
| 118 | + { |
| 119 | + name: "MultiContent with reasoning_content", |
| 120 | + jsonData: `{"role":"assistant","content":[{"type":"text","text":"Hello"}],"reasoning_content":"Multi reasoning"}`, |
| 121 | + expected: "Multi reasoning", |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "MultiContent with reasoning", |
| 125 | + jsonData: `{"role":"assistant","content":[{"type":"text","text":"Hello"}],"reasoning":"Multi reasoning"}`, |
| 126 | + expected: "Multi reasoning", |
| 127 | + }, |
| 128 | + } |
| 129 | + |
| 130 | + for _, tt := range tests { |
| 131 | + t.Run(tt.name, func(t *testing.T) { |
| 132 | + var msg openai.ChatCompletionMessage |
| 133 | + err := json.Unmarshal([]byte(tt.jsonData), &msg) |
| 134 | + if err != nil { |
| 135 | + t.Fatalf("Failed to unmarshal JSON: %v", err) |
| 136 | + } |
| 137 | + |
| 138 | + if msg.ReasoningContent != tt.expected { |
| 139 | + t.Errorf("Expected ReasoningContent to be %q, got %q", tt.expected, msg.ReasoningContent) |
| 140 | + } |
| 141 | + }) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// TestChatCompletionStreamChoiceDelta_MarshalJSON tests that marshaling preserves |
| 146 | +// the reasoning_content field name. |
| 147 | +func TestChatCompletionStreamChoiceDelta_MarshalJSON(t *testing.T) { |
| 148 | + delta := openai.ChatCompletionStreamChoiceDelta{ |
| 149 | + Role: "assistant", |
| 150 | + Content: "Hello", |
| 151 | + ReasoningContent: "Test reasoning", |
| 152 | + } |
| 153 | + |
| 154 | + data, err := json.Marshal(delta) |
| 155 | + if err != nil { |
| 156 | + t.Fatalf("Failed to marshal delta: %v", err) |
| 157 | + } |
| 158 | + |
| 159 | + // Verify that it's marshaled as reasoning_content |
| 160 | + var result map[string]interface{} |
| 161 | + err = json.Unmarshal(data, &result) |
| 162 | + if err != nil { |
| 163 | + t.Fatalf("Failed to unmarshal result: %v", err) |
| 164 | + } |
| 165 | + |
| 166 | + if _, hasReasoning := result["reasoning"]; hasReasoning { |
| 167 | + t.Error("Marshaled JSON should not contain 'reasoning' field") |
| 168 | + } |
| 169 | + |
| 170 | + if reasoningContent, ok := result["reasoning_content"].(string); !ok || reasoningContent != "Test reasoning" { |
| 171 | + t.Errorf("Expected reasoning_content to be 'Test reasoning', got %v", result["reasoning_content"]) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +// TestRealWorldStreamingResponse tests parsing a real-world streaming response |
| 176 | +// with reasoning field (similar to the new_api_output.txt format). |
| 177 | +func TestRealWorldStreamingResponse(t *testing.T) { |
| 178 | + // Simulate a chunk from the new_api_output.txt file |
| 179 | + jsonData := `{ |
| 180 | + "id":"gen-1763431956-test", |
| 181 | + "provider":"Azure", |
| 182 | + "model":"openai/gpt-5", |
| 183 | + "object":"chat.completion.chunk", |
| 184 | + "created":1763431956, |
| 185 | + "choices":[{ |
| 186 | + "index":0, |
| 187 | + "delta":{ |
| 188 | + "role":"assistant", |
| 189 | + "content":"", |
| 190 | + "reasoning":"quantum" |
| 191 | + }, |
| 192 | + "finish_reason":null, |
| 193 | + "logprobs":null |
| 194 | + }] |
| 195 | + }` |
| 196 | + |
| 197 | + var response openai.ChatCompletionStreamResponse |
| 198 | + err := json.Unmarshal([]byte(jsonData), &response) |
| 199 | + if err != nil { |
| 200 | + t.Fatalf("Failed to unmarshal streaming response: %v", err) |
| 201 | + } |
| 202 | + |
| 203 | + if len(response.Choices) != 1 { |
| 204 | + t.Fatalf("Expected 1 choice, got %d", len(response.Choices)) |
| 205 | + } |
| 206 | + |
| 207 | + delta := response.Choices[0].Delta |
| 208 | + if delta.ReasoningContent != "quantum" { |
| 209 | + t.Errorf("Expected ReasoningContent to be 'quantum', got %q", delta.ReasoningContent) |
| 210 | + } |
| 211 | + |
| 212 | + if delta.Role != "assistant" { |
| 213 | + t.Errorf("Expected Role to be 'assistant', got %q", delta.Role) |
| 214 | + } |
| 215 | +} |
0 commit comments