# `LlmCore.LLM.OpenAI`
[🔗](https://github.com/fosferon/llm_core/blob/v0.9.7/lib/llm_core/llm/openai.ex#L1)

OpenAI-compatible API provider implementing the Provider behaviour.

Works with OpenAI, OpenRouter, Together, Groq, local vLLM — any endpoint
that speaks the OpenAI chat completions format.

## Configuration

Defaults to OpenAI. Override per-call via opts or globally via app config:

    # Per-call
    OpenAI.send(prompt, base_url: "https://openrouter.ai/api/v1",
                        api_key: System.get_env("OPENROUTER_API_KEY"),
                        model: "anthropic/claude-sonnet-4-20250514")

    # Global (application config)
    config :llm_core, :openai_base_url, "https://openrouter.ai/api/v1"
    config :llm_core, :openai_api_key, System.get_env("OPENROUTER_API_KEY")

## Auth Resolution Order

1. `opts[:api_key]` (per-call)
2. `Application.get_env(:llm_core, :openai_api_key)`
3. the credential `OPENAI_API_KEY`, through `LlmCore.Credentials.lookup/1` (the OS environment
   by default; see `LlmCore.Credentials` for a host-supplied resolver)

## URL Resolution Order

1. `opts[:base_url]` (per-call)
2. `Application.get_env(:llm_core, :openai_base_url)`
3. `"https://api.openai.com/v1"` (default)

# `stream_event`

```elixir
@type stream_event() :: String.t() | {:usage, map()} | {:error, LlmCore.LLM.Error.t()}
```

# `available?`

```elixir
@spec available?() :: boolean()
```

Checks if an OpenAI-compatible API key is configured.

# `available?`

```elixir
@spec available?(map()) :: boolean()
```

Checks availability using the TOML-resolved auth config.

When a provider alias (e.g. zai) reuses this module with a different
`api_key_env`, this callback checks the correct env var instead of
the hardcoded `OPENAI_API_KEY` default.

# `capabilities`

```elixir
@spec capabilities() :: LlmCore.LLM.Provider.capabilities()
```

Returns the OpenAI capability map including streaming, structured output,
tool use, vision, and supported models.

# `provider_type`

```elixir
@spec provider_type() :: :api
```

Returns `:api` — OpenAI is a cloud API provider.

# `send`

```elixir
@spec send(LlmCore.LLM.Provider.prompt(), keyword()) ::
  {:ok, LlmCore.LLM.Response.t()} | {:error, LlmCore.LLM.Error.t()}
```

Sends a prompt to the OpenAI-compatible chat completions endpoint.

When `opts[:tools]` contains a list of `LlmToolkit.Tool` structs, tool
definitions are encoded into the request body. If the model responds
with `finish_reason: "tool_calls"`, the returned `Response.tool_calls`
will contain decoded `LlmToolkit.Tool.Call` structs.

`opts[:response_format]` requests structured output:

  * `{:json_schema, schema}` or `{:json_schema, schema, name: ..., strict: ...}` is sent as
    `response_format: %{type: "json_schema", json_schema: %{name, schema, strict}}` (`strict`
    defaults to `true`; the name defaults to the schema's `title`, else `"structured_output"`)
  * `:json` (or `:json_object`) is sent as `response_format: %{type: "json_object"}`
  * a `{:json_schema, schema}` whose schema is not a map (a module, say) cannot be sent; it is
    requested as `:json`, with a warning logged once

If the endpoint answers 400 rejecting that format (its error message or `param` names the
response format, JSON schema/object or structured output), the call is retried once with
`json_object` and the schema in a system message, and if that is rejected too, once more with
the instruction in the prompt only. The reply content is decoded (a surrounding Markdown code fence is tolerated)
into `Response.structured`, which is `nil` if it is not valid JSON;
`Response.metadata.response_format_mode` records the mode that answered. Calls without
`:response_format` are unchanged.

# `stream`

```elixir
@spec stream(LlmCore.LLM.Provider.prompt(), keyword()) ::
  {:ok, Enumerable.t()} | {:error, LlmCore.LLM.Error.t()}
```

Streams a response from the OpenAI-compatible chat completions endpoint.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
