Skip to main content

Overview

OpenAILLMService provides chat completion capabilities using OpenAI’s API, supporting streaming responses, function calling, vision input, and advanced context management for conversational AI applications with state-of-the-art language models.

OpenAI LLM API Reference

Pipecat’s API methods for OpenAI integration

Example Implementation

Function calling example with weather API

OpenAI Documentation

Official OpenAI API documentation

OpenAI Platform

Access models and manage API keys

Installation

To use OpenAI services, install the required dependencies:

Prerequisites

OpenAI Account Setup

Before using OpenAI LLM services, you need:
  1. OpenAI Account: Sign up at OpenAI Platform
  2. API Key: Generate an API key from your account dashboard
  3. Model Selection: Choose from available models (GPT-4.1, GPT-4o, GPT-4o-mini, etc.)
  4. Usage Limits: Set up billing and usage limits as needed

Required Environment Variables

  • OPENAI_API_KEY: Your OpenAI API key for authentication

Configuration

str
default:"None"
deprecated
Deprecated since 0.0.105, removed in 2.0.0. Use settings=OpenAILLMService.Settings(...) instead.
str
default:"None"
OpenAI API key. If None, uses the OPENAI_API_KEY environment variable.
str
default:"None"
Custom base URL for the OpenAI API. Override for proxied or self-hosted deployments.
str
default:"None"
OpenAI organization ID.
str
default:"None"
OpenAI project ID.
Mapping[str, str]
default:"None"
Additional HTTP headers to include in every request.
str | None
default:"None"
Service tier to request, such as "auto", "flex", or "priority". When None, the parameter is omitted and OpenAI’s default tier applies.
OpenAILLMService.Settings
default:"None"
Runtime-configurable model settings. See Settings below.
InputParams
default:"None"
deprecated
Deprecated since 0.0.105, removed in 2.0.0. Use settings=OpenAILLMService.Settings(...) instead.
float
default:"5.0"
Request timeout in seconds. Used when retry_on_timeout is enabled to determine when to retry.
bool
default:"False"
Whether to retry the request once if it times out. The retry attempt has no timeout limit.

Settings

Runtime-configurable settings passed via the settings constructor argument using OpenAILLMService.Settings(...). These can be updated mid-conversation with LLMUpdateSettingsFrame. See Service Settings for details.
NOT_GIVEN values are omitted from the API request entirely, letting the OpenAI API use its own defaults. This is different from None, which would be sent explicitly.

Usage

Basic Setup

With Custom Settings

Updating Settings at Runtime

Model settings can be changed mid-conversation using LLMUpdateSettingsFrame:

Notes

  • OpenAI-compatible providers: Many third-party LLM providers offer OpenAI-compatible APIs. You can use OpenAILLMService with them by setting base_url to the provider’s endpoint.
  • Retry behavior: When retry_on_timeout=True, the first attempt uses the retry_timeout_secs timeout. If it times out, a second attempt is made with no timeout limit.
  • Function calling: Supports OpenAI’s tool/function calling format. Register function handlers with llm.register_function() to handle tool calls automatically.
  • System instruction precedence: If both the system_instruction setting and an initial system message in the context are set, both are sent, with system_instruction first, and a warning is logged.

Event Handlers

OpenAILLMService supports the following event handlers, inherited from LLMService: