Create Chat Completion
Create an AI chat conversation and get the model-generated chat response data
Endpoint
POST https://api.stepfun.ai/v1/chat/completions
Request Parameters
-
modelstringrequired
Name of the model to use -
messagesobject arrayrequired
List of different categories of messages entered by the user or generated by the model so farExpand/Collapse
- System message
objectExpand/Collapse
-
rolestringRole name for system messages, always
system -
contentstringText content of the system message
-
- User message
objectExpand/Collapse
rolestring
Role name for user messages, alwaysusercontentstring or object array
User message content, either amultipartmessage list or a plain text message stringExpand/Collapse
- Plain text message
string multipartmessage listobject array
Structured list of mixed image, video, audio, and text messagesExpand/Collapse
- Text message
objectExpand/Collapse
typestring
Alwaystexttextstring
Message text content
- Image message
objectExpand/Collapse
typestring
Alwaysimage_urlimage_urlobjectExpand/Collapse
urlstringImage URL or base64-encoded image. Supported formats: jpg/jpeg, png, webp, static gif; only http and https are allowed.
Example base64 format:data:image/jpeg;base64,${base64_string}. Replace the image format (jpeg) and the corresponding base64 string as needed.detailstringoptionalWhether to enable detail mode: choose
low/high, default islow.lowmode scales the image to a fixed size with a token count around 400.highmode provides more detailed and richer information. It performs better on large images, OCR, or extreme aspect ratios. Token count varies by the image.
- Video message
objectExpand/Collapse
typestring
Alwaysvideo_urlvideo_urlobjectExpand/Collapse
urlstringURL of the video. Format: video/mp4; only http and https are supported. Video must be under 128 MB and the duration is recommended to be under 5 minutes.
- Audio message
objectExpand/Collapse
typestring
Alwaysinput_audioinput_audioobjectExpand/Collapse
datastringAudio content, provided as a base64-encoded audio file.
Example base64 format:data:audio/mpeg;base64,${base64_string}. Replace the audio format (currently only mp3 and wav are supported) and its corresponding base64 string.
- Text message
- Plain text message
- Tool function message
objectExpand/Collapse
rolestring
Role name for tool messages, alwaystoolcontentstring
Content returned after executing the functiontool_call_idstring
ID of the executed function, returned by the assistant in the previous turn.
- Chat assistant message
objectExpand/Collapse
rolestring
Role name for assistant messages, alwaysassistantcontentstring | null
Text content of the assistant message
- System message
-
toolsobject arrayoptional
List of functions supported for ToolcallExpand/Collapse
typestring
Tool type, always
functionfunctionobject
Description of the function
Expand/Collapse
namestring
Function name. Only English letters, numbers, and _- are allowed; recommended to keep it under 64 characters.
descriptionstring
Function description, supports Chinese and English. It tells the model what the function does so the model can judge and choose.
parametersobject
Parameters of the function
Expand/Collapse
typeobject
Parameter description, usually object
propertiesobject
Function parameter definitions, with the key as the parameter name, and
typeanddescriptiondescribing the type and details.Expand/Collapse
typestring|number|integer|object|array|boolean
Parameter type. See json-schema for reference.
descriptionstring
Parameter description, supports Chinese and English. It tells the model what each parameter means.
-
max_tokensintoptional
Maximum number oftokensto generate for the chat. Default is INF (no limit, determined automatically by the model). The total of input and generatedtokensis limited by the specified model’s maximum context length. -
temperaturefloatoptional
Sampling temperature, a number between 0.0 and 2.0. Higher values (e.g., 0.8) make the output more random; lower values (e.g., 0.2) make it more focused and deterministic. Default is 0.5. -
top_pfloatoptional
Top-p sampling. The model generatestokenswithin the probability mass of top_p and outputs them. Default is 0.9. -
nintoptional
Controls how many response messages the model generates for each input message. Default is 1; no hard max, but suggested up to 5. -
streambooloptional
Whether to stream the response messages. Default is false. -
stopstring | string arrayoptional
Stops generation when encountering any of the stop content. Empty by default. -
frequency_penaltyfloatoptional
Default is 0. A number between 0.0 and 1.0. Higher values penalize tokens that have appeared frequently in the generated text, reducing repetition. -
response_formatobjectoptional
Used to instruct the model to output in a specific format. Default is{"type":"text"}, meaning text output. Set{ "type": "json_object" }to turn on JSON Mode and output parseable JSON. -
reasoning_formatobjectoptional
Used to instruct the model which reasoning field to use when outputting; default isgeneral, meaning general reasoning that returns areasoningfield. Options are [general,deepseek-style]. When set todeepseek-style, you can use the DeepSeek-compatiblereasoning_contentfield to obtain the reasoning content.
Response
Returns Chat Completion response object, or Chat Completion streaming response object chunk
Examples
from openai import OpenAI
client = OpenAI(api_key = "STEP_API_KEY", base_url = "https://api.stepfun.ai/v1")
completion = client.chat.completions.create(
model = "step-3.5-flash",
messages = [
{
"role": "system",
"content": "You are an AI chat assistant provided by StepFun. You are good at Chinese, English, and many other languages. While keeping user data safe, you can respond quickly and accurately to users' questions and requests. Your answers and suggestions should reject content related to pornography, gambling, drugs, violence, or terrorism.",
},
{
"role": "user",
"content": "Hello, please introduce StepFun's artificial intelligence!"
}, ],
)
print(completion)
{
"id": "b7b56af0-52a6-483f-a589-948182676a1b",
"object": "chat.completion",
"created": 1709893411,
"model": "step-3.5-flash",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! StepFun is a company focused on artificial intelligence technology and is dedicated to developing and providing various AI solutions. Our AI capabilities cover natural language processing, computer vision, and machine learning to help users in many industries improve efficiency and create value.\n\nWe provide a variety of AI products and services, including intelligent customer service, virtual assistants, smart recommendations, and intelligent moderation. These products and services can be applied across industries such as finance, retail, education, and healthcare. By using our AI, users can better understand and analyze data, deliver personalized services and experiences, and improve the efficiency and accuracy of their decisions.\n\nWe value the security and privacy of user data and strictly follow relevant laws, regulations, and industry standards. We believe AI technology should create more benefits for people rather than negative impacts. We will continue to work hard to provide smarter, more efficient, and safer AI solutions."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 83,
"completion_tokens": 176,
"total_tokens": 259
}
}