Skip to main content
Generate text from a prompt or conversation with the chat completions API. This API allows you to send text, images, and videos with your request. Learn about different ways to use the API throughout this page. For information on all request and response fields, see the chat completions API reference. For a list of models and their supported features and modalities, see Supported models.
This API supports the OpenAI Chat Completions interface.To try a Modular endpoint with an existing OpenAI SDK integration, set base_url to https://api.modular.com/v1 and api_key to your Modular API key.

Generate a text response

Generate text from a prompt or conversation history.
Send a chat completion request using the OpenAI Python SDK:
generate-text.py
The response should be similar to:

Stream a text response

Set stream=True to receive tokens as they are generated instead of waiting for the full response.
Set stream=True and iterate over the response chunks as they arrive:
stream-text.py

Analyze an image

Provide an image and a text prompt to generate a description of or extract information from visual content. Include the image in the messages array using an image_url content item. The url field accepts either a publicly accessible URL or a base64-encoded data URI (for example, data:image/jpeg;base64,...). The following example asks the model to describe the contents of an image.
Include an image_url content block alongside your text prompt:
generate-image-description.py
The response should be similar to:

Analyze a video

Provide a video and a text prompt to generate a natural-language description or analysis of the video’s visual content. Include the video in the messages array using a video_url content item. The url field accepts either a publicly accessible URL or a base64-encoded data URI (for example, data:video/mp4;base64,...). The following example asks the model to describe the contents of a video.
Pass a video URL and a text question to get a natural-language description of the video:
generate-video-description.py
The response should be similar to: