Skip to content

fix(messages): honor OpenAI stop_sequences (#37118) - #48534

Merged
ishaan-j-berri merged 1 commit into
mainfrom
litellm_fix_37118_messages_stop_sequences
Oct 27, 2026
Merged

ishaan-j-berri merged 1 commit into
mainfrom
litellm_fix_37118_messages_stop_sequences

Conversation

@ishaan-j-berri

@ishaan-j-berri ishaan-j-berri commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Refs #37118

OpenAI chat models now honor stop_sequences sent to /v1/messages. Requests with a nonempty stop list use Chat Completions, where the existing adapter forwards stop. Empty and omitted stop lists keep their current route

Details

Screenshots / Proof of Fix

Live OpenAI gpt-4o-mini, using the issue's model and prompt. The base checkout ran on port 4021 and the PR checkout on port 4019, with each checkout explicitly on PYTHONPATH. Both used this config and an OPENAI_API_KEY loaded from protected local storage

model_list:
  - model_name: gpt-mini
    litellm_params:
      model: openai/gpt-4o-mini
      api_key: os.environ/OPENAI_API_KEY
general_settings:
  dangerously_permit_weak_or_unset_master_key: true

The unauthenticated config above was restricted to 127.0.0.1. Save this request as messages.json

{"model":"gpt-mini","max_tokens":80,"stop_sequences":["STOPPROBE"],"messages":[{"role":"user","content":"Write the exact token STOPPROBE as the first word, then write a 40-word poem about cats. Do not skip the token."}]}

The response excerpts below retain the full content and stop fields and omit response IDs, model, and usage

Before (53a4cee)

  1. Send the request

    curl -sS --max-time 90 -w '\nHTTP %{http_code}\n' \
      http://127.0.0.1:4021/v1/messages \
      -H 'content-type: application/json' \
      -H 'anthropic-version: 2023-06-01' \
      --data-binary @messages.json
  2. Observe HTTP 200 with the stop token and poem

    {
      "content": [
        {
          "type": "text",
          "text": "STOPPROBE  \nWhiskers twitch in twilight's glow,  \nPaws dance softly, silent flow.  \nPurring echoes, dreams take flight,  \nChasing shadows in the night.  \nEyes like lanterns, bright and keen,  \nFeline mystique, a grace unseen.  \nSoftly curled, a peaceful sight."
        }
      ],
      "stop_reason": "end_turn",
      "stop_sequence": null
    }
    HTTP 200
    

After (4079e66)

  1. Send the same request

    curl -sS --max-time 90 -w '\nHTTP %{http_code}\n' \
      http://127.0.0.1:4019/v1/messages \
      -H 'content-type: application/json' \
      -H 'anthropic-version: 2023-06-01' \
      --data-binary @messages.json
  2. Observe HTTP 200 with empty text and no poem

    {
      "content": [
        {
          "type": "text",
          "text": ""
        }
      ],
      "stop_reason": "end_turn",
      "stop_sequence": null
    }
    HTTP 200
    

The same live requests with stream: true emitted the token and poem before the fix and no text deltas after it, followed by message_stop. The /v1/chat/completions control with stop: ["STOPPROBE"] returned empty text and finish_reason: stop at both commits

Caveats

OpenAI reports finish_reason: stop for both a natural end and a matched stop sequence, without identifying the sequence. This patch preserves the existing stop_reason: end_turn and stop_sequence: null mapping. It fixes the dropped stop request for chat-capable models, while the issue's requested stop_sequence response reason remains unresolved

The OSV CI scan fails on dependency findings in existing lockfiles, including filelock, Pillow, and the VS Code extension’s MSAL package. This PR changes no lockfiles or scanner configuration. Other CI jobs are still running

@codspeed

codspeed Bot commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 35 untouched benchmarks


Comparing litellm_fix_37118_messages_stop_sequences (4079e66) with main (53a4cee)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (e575793) during the generation of this report, so 53a4cee was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@ishaan-j-berri
ishaan-j-berri merged commit aa33b05 into main Oct 27, 2026
86 of 87 checks passed
@ishaan-j-berri
ishaan-j-berri deleted the litellm_fix_37118_messages_stop_sequences branch October 27, 2026 03:28
@codecov

codecov Bot commented Oct 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

2 participants