magpie icon indicating copy to clipboard operation
magpie copied to clipboard

Stop sequences are dropped when a request is translated to the Responses API

Open roeseth opened this issue 3 weeks ago • 1 comments

magpie 0.1.4094, Windows 11. Models served by a provider plugin on the Responses API (npm: "@ai-sdk/openai"); the upstream is Amazon Bedrock's /openai/v1/responses, but nothing below depends on that.

What I did

A Chat Completions request with stop, and an Anthropic Messages request with stop_sequences, to a model magpie sends on as a Responses request:

{"model": "bedrock-profile/gpt-5.5", "max_tokens": 1024, "stop": ["STOP"],
 "messages": [{"role": "user", "content": "Repeat exactly, with nothing else: one two STOP three"}]}
{"model": "bedrock-profile/gpt-5.5", "max_tokens": 1024, "stop_sequences": ["STOP"],
 "messages": [{"role": "user", "content": "Repeat exactly, with nothing else: one two STOP three"}]}

Streamed and buffered, on gpt-5.5 and grok-4.6. Eight requests in all.

What happened

Every reply was one two STOP three, with finish_reason: "stop" on Chat Completions and stop_reason: "end_turn" on Messages. The plugin logs the body its fetch receives: none of the 481 Responses bodies in that run carried a stop field, so the list is dropped during translation and the plugin has no way to apply it.

What I expected

The Responses API has no stop parameter, so the field cannot be passed through. Either of these keeps the contract the agent's API makes:

  • Emulate it: end the reply at the first match, with finish_reason: "stop" on Chat Completions and stop_reason: "stop_sequence" plus stop_sequence: "STOP" on Messages, and don't match across a tool call or a reasoning item. The gateway this replaced did exactly that.
  • Or refuse the request with a 400 that says stop sequences aren't supported on this model, rather than serving it without them.

Dropping the field silently is the worst of the three: agents that rely on a stop word to end a list or a template get text that runs past it, with no error to notice.

roeseth avatar Nov 14 '26 03:11 roeseth

gateway: pass stop sequences through to Responses API upstreams (#6592)

Fixes #6592

Root Cause

When translating incoming Chat Completions (stop), Anthropic Messages (stop_sequences), or native Responses API requests to a Responses API upstream, buildResponses in internal/gateway/responses.go omitted r.Stop from the constructed JSON payload. In addition, rRequest lacked a Stop field and readResponsesFor did not decode it, causing stop sequences to be completely discarded whenever a model was accessed via the Responses API protocol. As a result, upstream provider plugins (e.g. @ai-sdk/openai or Amazon Bedrock /openai/v1/responses) never received stop parameters and models continued generating text past the intended stopping markers.

Changes

  1. Added Stop json.RawMessage to rRequest in internal/gateway/responses.go.
  2. Updated readResponsesFor to decode q.Stop (handling both single string and string slice formats, aligned with chat.go).
  3. Updated buildResponses to include "stop": r.Stop when len(r.Stop) > 0.
  4. Added unit tests in internal/gateway/responses_stop_test.go covering buildResponses, parseResponses, and translation from both Chat Completions and Anthropic Messages to Responses requests with stop sequences.

Verification

  • Red before: Running go test -tags nogui -race ./internal/gateway -run 'TestBuildResponsesStop|TestParseResponsesStop' on the unmodified baseline failed as buildResponses got stop [], want [STOP END] and parseResponses got [].
  • Green after: All tests passed:
    === RUN   TestBuildResponsesStop
    --- PASS: TestBuildResponsesStop (0.00s)
    === RUN   TestParseResponsesStop
    --- PASS: TestParseResponsesStop (0.00s)
    === RUN   TestTranslateStopToResponses
    --- PASS: TestTranslateStopToResponses (0.00s)
    PASS
    ok      github.com/yetone/magpie/internal/gateway       1.491s
    
  • Static checks:
    • GOOS=linux go vet -tags nogui ./internal/gateway: PASS
    • GOOS=windows go vet ./internal/gateway: PASS
    • gofmt -l internal/gateway/responses.go internal/gateway/responses_stop_test.go: PASS
    • git diff --check: PASS

Co-authored-by: roeseth [email protected]

Gaoshou101 avatar Nov 14 '26 04:11 Gaoshou101