Stop sequences are dropped when a request is translated to the Responses API
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 andstop_reason: "stop_sequence"plusstop_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.
gateway: pass stop sequences through to Responses API upstreams (#6592)
- Fork branch:
Gaoshou101:fix/responses-stop-sequences-6592(commit24cd28c8a) - Compare: https://github.com/yetone/magpie/compare/main...Gaoshou101:fix/responses-stop-sequences-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
- Added
Stop json.RawMessagetorRequestininternal/gateway/responses.go. - Updated
readResponsesForto decodeq.Stop(handling both single string and string slice formats, aligned withchat.go). - Updated
buildResponsesto include"stop": r.Stopwhenlen(r.Stop) > 0. - Added unit tests in
internal/gateway/responses_stop_test.gocoveringbuildResponses,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 asbuildResponses got stop [], want [STOP END]andparseResponses 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: PASSGOOS=windows go vet ./internal/gateway: PASSgofmt -l internal/gateway/responses.go internal/gateway/responses_stop_test.go: PASSgit diff --check: PASS
Co-authored-by: roeseth [email protected]