openai.ChatClient

One model turn against OpenAI's Chat Completions API (`POST https://api.openai.com/v1/chat/completions`).

Reference version

Signature

class openai.ChatClient

One model turn against OpenAI's Chat Completions API (POST https://api.openai.com/v1/chat/completions).

The sibling of openai.ResponsesClient: same provider, older and far more widely-cloned endpoint. Reach for this one when you want chat-completions semantics from OpenAI itself; for any other OpenAI-compatible server use openai.GenericClient, openai.OllamaClient, openai.OpenRouterClient or openai.AzureClient, which are the same wire core with different compat flags (openai/ns_internal/chat.baml).

Source:<builtin>/openai/chat.bamlbytes 5536991

Fields

model

string

api_key

ai.Credential | null

base_url

ai.Credential | null

request_timeout_ms

int | null

time_to_first_token_timeout_ms

int | null

request_body

baml.json.json | null

headers

map<string, string> | null

query_params

map<string, string> | null

temperature

float | null

max_tokens

int | null

max_completion_tokens

int | null

top_p

float | null

stop

string[] | null

seed

int | null

capture_wire

bool

Static methods

function

new

(
model: string = …,
api_key: ai.Credential | null = …,
base_url: ai.Credential | null = …,
request_timeout_ms: int | null = …,
time_to_first_token_timeout_ms: int | null = …,
request_body: baml.json.json | null = …,
headers: map<string, string> | null = …,
query_params: map<string, string> | null = …,
temperature: float | null = …,
max_tokens: int | null = …,
max_completion_tokens: int | null = …,
top_p: float | null = …,
stop: string[] | null = …,
seed: int | null = …,
capture_wire: bool = …
) -> openai.ChatClient

Creates a client. Construction reads no environment variable, so declaring one is always safe; credentials and endpoints resolve when a request is built.

Instance methods

function

compat

(self) -> openai.internal.ChatCompat throws baml.errors.Io | baml.errors.ParseError

The provider record: everything the shared core needs to know about this endpoint. Only stream_options distinguishes first-party OpenAI from a bare compatible server. The provider record: everything the shared chat core needs to know about this endpoint.

function

params

(
self,
preview: bool = …
) -> openai.internal.ChatParams throws baml.errors.Io | baml.errors.ParseError

The per-request parameters handed to the shared chat core.

Parameters

  • preview: omit the credential, for rendering a request without sending it.
function

resolved_api_key

(self) -> string | null throws baml.errors.Io | baml.errors.ParseError

The request-time credential: the explicit value (a literal, or the value of the variable an env.NAME ref points at), else the provider's canonical variable. Null is reported as a configuration error when the request is built, naming OPENAI_API_KEY.

function

resolved_base_url

(self) -> string throws baml.errors.Io | baml.errors.ParseError

The request-time base URL, or the provider default.

Implementations

ai.Client for openai.ChatClient

Instance methods

function

id

(self) -> string
function

invoke

(
self,
input: ai.ModelTurnInput
) -> ai.ModelTurn throws baml.errors.Timeout | baml.errors.UnknownError | reflect.errors.CompilationError | ai.errors.Failure
function

render

(
self,
input: ai.ModelTurnInput
) -> baml.http.Request throws baml.errors.Timeout | baml.errors.UnknownError | reflect.errors.CompilationError | ai.errors.Failure

Source:<builtin>/openai/chat.bamlbytes 56076409

ai.stream.StreamingClient for openai.ChatClient

Instance methods

function

invoke_stream

(
self,
input: ai.ModelTurnInput
) -> ai.stream.TurnStream throws baml.errors.Timeout | baml.errors.UnknownError | reflect.errors.CompilationError | ai.errors.Failure

Source:<builtin>/openai/chat.bamlbytes 64156989