Skip to main content

Overview

The completion() function provides a unified interface to call 100+ LLM providers. It translates OpenAI-format requests to provider-specific formats and returns standardized responses.

Basic Usage

Function Signature

Parameters

string
required
The model to use for completion. Examples: gpt-4, claude-3-5-sonnet-20241022, gemini-pro
List[Dict[str, str]]
required
List of messages in the conversation. Each message should have role and content fields.

Optional Parameters

float
Controls randomness in the output (0.0 to 2.0). Lower values make output more focused and deterministic.
int
Maximum number of tokens to generate in the completion.
bool
If True, returns a streaming response. Default: False
List[Dict]
List of tools (functions) the model can call. See Function Calling for details.
Union[dict, Type[BaseModel]]
Specify the output format. Can be a dict with {"type": "json_object"} or a Pydantic model.
float
Request timeout in seconds. Default: 600 (10 minutes)
str
API key for the provider. If not provided, reads from environment variables.
str
Custom API base URL for the provider.

Response Format

The function returns a ModelResponse object with the following structure:

Examples

Basic Completion

Using Different Providers

Structured Output with JSON

Structured Output with Pydantic

System Messages and Context

Setting Timeouts

Error Handling

Return Types

Non-Streaming Response

Returns a ModelResponse object:

Streaming Response

Returns a CustomStreamWrapper object. See Streaming for details.