internal/translator/openai/interactions/chat-completions: convertOpenAIChatStreamToInteractions prematurely closes step on finish_reason: null

Open Beginner friendly
#7,973 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
api

Research direction

Start in internal/translator/openai/interactions/chat-completions/interactions_openai_response.go at lines 140-142, inside convertOpenAIChatStreamToInteractions, where the finish_reason existence check fires on JSON null. Read how ojson values expose their type before tightening the guard. Reproduce with go test -v -race -count=1 -run TestRepro_OpenAIChatStreamNullFinishReasonClosesInteractionsStep ./internal/translator/openai/interactions/chat-completions; done means step.stop is not emitted for finish_reason: null chunks and only fires on a non-empty string reason.

Written by the indexing model from the issue text.

Description

Summary

In internal/translator/openai/interactions/chat-completions/interactions_openai_response.go:140-142, convertOpenAIChatStreamToInteractions checks if finishReason := choice.Get("finish_reason"); finishReason.Exists() without verifying finishReason.Type != ojson.Null && finishReason.String() != "". In OpenAI Chat Completions SSE streams, every non-terminal chunk carries "finish_reason": null, so Exists() returns true and appendInteractionsStepStop fires on every chunk, closing step 0 prematurely and forcing subsequent deltas into new step.start events.

Environment
  • CLIProxyAPI revision: origin/dev at 32842348 (confirmed on v8.1.2)
  • OS / Go: Windows 11 amd64, go1.27.4
  • Route: Interactions SSE -> OpenAI Chat Completions -> ConvertOpenAIResponseToInteractions
Minimal Reproduction & Observed vs Expected
go test -v -race -count=1 -run TestRepro_OpenAIChatStreamNullFinishReasonClosesInteractionsStep ./internal/translator/openai/interactions/chat-completions

Observed output (origin/dev 32842348):

=== RUN   TestRepro_OpenAIChatStreamNullFinishReasonClosesInteractionsStep
    repro_v812_test.go:18: finish_reason:null prematurely emitted step.stop:
        ...
        event: step.stop
        data: {"index":0,"event_type":"step.stop"}
--- FAIL: TestRepro_OpenAIChatStreamNullFinishReasonClosesInteractionsStep (0.00s)
Check / Field Observed (origin/dev 32842348) Expected
Chunk with "finish_reason": null Emits step.stop on every chunk Keeps step 0 open until non-empty finish_reason
Proposed fix (internal/translator/**)

Guard interactions_openai_response.go:140 with if finishReason := choice.Get("finish_reason"); finishReason.Exists() && finishReason.Type == ojson.String && strings.TrimSpace(finishReason.String()) != "".

What ran / Not run
  • Ran: Hermetic go test -v -race -count=1 -run TestRepro_OpenAIChatStreamNullFinishReasonClosesInteractionsStep ./internal/translator/openai/interactions/chat-completions against origin/dev (32842348).
  • Not run: Live upstream calls.
Dominant language
Go
Stars
58.8k
Forks
9k
Avg merge
14h 20m
Merged PRs (30d)
67

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from router-for-me/CLIProxyAPI

All issues in router-for-me/CLIProxyAPI

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.