Skip to content

Fix missing model field in first streaming response for OpenAI API compatibility#1

Closed
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-issue-in-fork
Closed

Fix missing model field in first streaming response for OpenAI API compatibility#1
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-issue-in-fork

Conversation

Copy link
Copy Markdown

Copilot AI commented Oct 17, 2025

Problem

The OpenAI Chat Completions API requires that each Server-Sent Event (SSE) response contains the fields choices, id, and model. However, the first streaming response from this proxy was missing the model field, causing compatibility issues with OpenAI-compatible clients.

Here's an example of what the first response looked like:

{
  "choices": [],
  "created": 0,
  "id": "",
  "prompt_filter_results": [...]
}

Notice the missing model field that should be present according to the OpenAI API specification.

Root Cause

In src/routes/chat-completions/handler.ts, the streaming handler was incorrectly passing raw SSE event objects directly to stream.writeSSE() without extracting the data field:

for await (const chunk of response) {
  await stream.writeSSE(chunk as SSEMessage)
}

The variable chunk is actually a ServerSentEventMessage object from fetch-event-stream with the structure:

{
  data?: string,      // The actual JSON payload (as a string)
  event?: string,
  id?: string | number,
  ...
}

By casting and passing this raw event object directly, the handler was sending malformed SSE events to clients. The actual chat completion data (which includes the model field) is in the data property as a JSON string.

Solution

This PR fixes the issue by properly extracting the data field from raw SSE events before forwarding them to clients:

for await (const rawEvent of response) {
  if (rawEvent.data === "[DONE]") {
    await stream.writeSSE({ data: "[DONE]" })
    break
  }
  if (!rawEvent.data) {
    continue
  }
  await stream.writeSSE({ data: rawEvent.data })
}

This approach:

  1. Extracts the data field which contains the properly formatted JSON from GitHub Copilot (including the model field)
  2. Properly handles the [DONE] sentinel that marks the end of the stream
  3. Skips events with empty data
  4. Aligns with the pattern already used in src/routes/messages/handler.ts for Anthropic API streaming

Changes

  • Modified src/routes/chat-completions/handler.ts to properly parse and forward SSE events
  • Removed unused SSEMessage type import
  • Renamed variable from chunk to rawEvent for clarity

Testing

  • ✅ All 26 existing tests pass
  • ✅ Build succeeds with no errors
  • ✅ Linting passes with no issues
  • ✅ TypeScript type checking passes

Fixes ericc-ch#133

Original prompt

@ericc-ch/copilot-apiericc-ch/issues/133 , fix the issue in fork.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Fix issue in fork of copilot API Fix missing model field in first streaming response for OpenAI API compatibility Oct 17, 2025
Copilot AI requested a review from Eason0729 October 17, 2025 15:05
@Eason0729 Eason0729 closed this Oct 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

model is missing in first streaming response.

2 participants