Skip to main content
The openai provider supports the OpenAI /chat endpoint with OpenAI-specific default configuration options.
For Azure OpenAI, use the azure provider instead.For other OpenAI-compatible providers (Groq, Ollama, OpenRouter, Together AI, etc.), use openai-compatible.

Quick Start

client<llm> MyClient {
  provider "openai"
  options {
    api_key env.MY_OPENAI_KEY
    model "gpt-5-mini"
    temperature 0.1
  }
}

Authentication

Set your OpenAI API key as an environment variable:
export OPENAI_API_KEY="your-api-key-here"
Or specify it explicitly in your BAML configuration:
client<llm> MyClient {
  provider "openai"
  options {
    api_key env.MY_OPENAI_KEY
    model "gpt-5-mini"
  }
}

Configuration Options

BAML-Specific Options

These options modify the API request sent to OpenAI.
api_key
string
default:"env.OPENAI_API_KEY"
Used to build the Authorization header: Authorization: Bearer $api_key
base_url
string
default:"https://api.openai.com/v1"
The base URL for the OpenAI API. Override this to use different regions or custom endpoints.Example for EU region:
client<llm> EUClient {
  provider "openai"
  options {
    base_url "https://eu.api.openai.com/v1"
    model "gpt-5-mini"
  }
}
headers
object
Additional headers to send with requests.
client<llm> MyClient {
  provider "openai"
  options {
    model "gpt-5-mini"
    headers {
      "X-My-Header" "my-value"
    }
  }
}

Supported Models

model
string
required
The OpenAI model to use.
ModelUse CaseContextKey Features
gpt-5Coding, agentic tasks, expert reasoning400K totalBuilt-in reasoning, 45% fewer errors
gpt-5-miniWell-defined tasks, cost-efficient400K totalFaster alternative to GPT-5
gpt-5-nanoLightweight tasks, maximum efficiency400K totalMost cost-effective GPT-5 variant
gpt-4.1Large-scale technical work1MEnhanced coding, instruction following
gpt-4.1-miniBalanced performance and cost1MReplaces GPT-4o mini
gpt-4.1-nanoLightweight variant1MBudget-friendly option
gpt-4oGeneral purpose, multimodal200KUpdated knowledge cutoff June 2024
You can specify any model name - BAML won’t validate whether it exists.

Model Parameters

These parameters are passed directly to the OpenAI API.
For reasoning models like o1 or o1-mini, use max_completion_tokens instead of max_tokens. Set max_tokens to null.See the OpenAI API documentation for details.
client<llm> OpenAIo1 {
  provider "openai"
  options {
    model "o1-mini"
    max_tokens null
    max_completion_tokens 4096
  }
}
Common parameters:
  • temperature - Controls randomness (0-2)
  • max_tokens - Maximum tokens to generate
  • top_p - Nucleus sampling parameter
  • frequency_penalty - Reduces repetition (-2.0 to 2.0)
  • presence_penalty - Encourages new topics (-2.0 to 2.0)
For all options, see the OpenAI API documentation.

Regional Endpoints

To access OpenAI’s API in different regions:
client<llm> EUClient {
  provider "openai"
  options {
    base_url "https://eu.api.openai.com/v1"
    model "gpt-5-mini"
  }
}

Features

  • Streaming: Supported for real-time response generation
  • Multimodal: Supports text and image inputs
  • Function Calling: Native support for tool use and function calling
  • JSON Mode: Structured output support

Do Not Set

messages
DO NOT USE
BAML automatically constructs this from your prompt.
stream
DO NOT USE
BAML automatically sets this based on how you call the client in your code.

Build docs developers (and LLMs) love