From bc74763e923b764c52a5ac756e85a7ee8b1583b5 Mon Sep 17 00:00:00 2001 From: Piotr Stankiewicz Date: Mon, 25 Aug 2025 13:04:34 +0200 Subject: [PATCH] metrics: Record reasoning_content from streaming responses Signed-off-by: Piotr Stankiewicz --- pkg/metrics/openai_recorder.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/metrics/openai_recorder.go b/pkg/metrics/openai_recorder.go index d3d1194..3f4f724 100644 --- a/pkg/metrics/openai_recorder.go +++ b/pkg/metrics/openai_recorder.go @@ -190,6 +190,7 @@ func (r *OpenAIRecorder) RecordResponse(id, model string, rw http.ResponseWriter func (r *OpenAIRecorder) convertStreamingResponse(streamingBody string) string { lines := strings.Split(streamingBody, "\n") var contentBuilder strings.Builder + var reasoningContentBuilder strings.Builder var lastChunk map[string]interface{} for _, line := range lines { @@ -212,6 +213,9 @@ func (r *OpenAIRecorder) convertStreamingResponse(streamingBody string) string { if content, ok := delta["content"].(string); ok { contentBuilder.WriteString(content) } + if content, ok := delta["reasoning_content"].(string); ok { + reasoningContentBuilder.WriteString(content) + } } } } @@ -230,10 +234,14 @@ func (r *OpenAIRecorder) convertStreamingResponse(streamingBody string) string { if choices, ok := finalResponse["choices"].([]interface{}); ok && len(choices) > 0 { if choice, ok := choices[0].(map[string]interface{}); ok { - choice["message"] = map[string]interface{}{ + message := map[string]interface{}{ "role": "assistant", "content": contentBuilder.String(), } + if reasoningContentBuilder.Len() > 0 { + message["reasoning_content"] = reasoningContentBuilder.String() + } + choice["message"] = message delete(choice, "delta") if _, ok := choice["finish_reason"]; !ok {