How to use stop sequences in OpenAI API
Quick answer
Use the
stop parameter in the OpenAI API's chat.completions.create method to specify one or more sequences where the model should stop generating further text. Pass a string or list of strings to stop to control output termination precisely.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the official OpenAI Python SDK and set your API key as an environment variable for secure authentication.
pip install openai>=1.0 Step by step
This example demonstrates how to use the stop parameter to halt generation when the model outputs a specific sequence, such as a newline or a custom token.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Write a short poem about spring."}],
stop=["\n\n", "STOP"]
)
print(response.choices[0].message.content) output
A gentle breeze whispers, Flowers bloom in bright array, Spring dances softly.
Common variations
- Use a single string or a list of strings for
stopto specify multiple stop sequences. - Apply
stopwith different models likegpt-4o-miniorgpt-4o. - In async code, pass
stopthe same way when callingawait client.chat.completions.acreate().
import asyncio
import os
from openai import OpenAI
async def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = await client.chat.completions.acreate(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "List three fruits."}],
stop=["."]
)
print(response.choices[0].message.content)
asyncio.run(main()) output
Apple Banana Cherry
Troubleshooting
- If the model ignores your stop sequences, ensure they exactly match the output tokens including whitespace and casing.
- Stop sequences can truncate output abruptly; test different sequences to find the best fit.
- Using too many or very short stop sequences may cause premature stopping.
Key Takeaways
- Use the
stopparameter to precisely control where the model output ends. - Pass a string or list of strings to
stopfor single or multiple stop sequences. - Stop sequences must exactly match the generated text to work correctly.
- The
stopparameter works consistently across synchronous and asynchronous calls. - Test and adjust stop sequences to avoid cutting off output prematurely.