Skip to main content

Module openai

Module openai 

Source
Expand description

The OpenAI provider, behind the openai feature.

OpenAIModel streams from the Responses API by default, or from Chat Completions with OpenAIApi::Chat, over HTTP with the official OpenAI SDK’s transport behaviour (retries, timeout, error texts). It maps every content block, the system prompt, tool specs and tool choice, prompt caching (CacheConfig), server-managed conversation state on the Responses API (OpenAIModelConfig::stateful) and OpenAI’s built-in tools, such as the native web_search, through OpenAIModelConfig::params. With the mantle feature, OpenAIModel::bedrock_mantle routes the model through Amazon Bedrock’s OpenAI-compatible Mantle endpoint.

use strands::models::openai::{OpenAIApi, OpenAIModel};
use strands::models::Model;

// Responses API (default)
let model = OpenAIModel::builder().model_id("gpt-5.4").api_key("sk-...").build()?;
assert_eq!(model.model_id().as_deref(), Some("gpt-5.4"));

// Chat Completions
let chat = OpenAIModel::builder()
    .api(OpenAIApi::Chat)
    .model_id("gpt-5.4")
    .api_key("sk-...")
    .build()?;
assert_eq!(chat.api(), OpenAIApi::Chat);

An agent on the Responses API with the built-in web search (the API key comes from OPENAI_API_KEY):

use serde_json::json;
use strands::Agent;
use strands::models::openai::OpenAIModel;

let model = OpenAIModel::builder()
    .model_id("gpt-5.4")
    .params(json!({"tools": [{"type": "web_search"}]}))
    .build()?;
let mut agent = Agent::builder().model(model).build()?;
let result = agent.invoke("What changed in the latest Rust release?").await?;
println!("{result}");

Structs§

ApiKeySetter
A function returning an API key, called before each request (the SDK’s ApiKeySetter).
BedrockMantleConfig
Routes an OpenAIModel through Amazon Bedrock’s Mantle endpoint (BedrockMantleConfig).
Credentials
AWS SDK Credentials
OpenAIClientConfig
How the client reaches the OpenAI API.
OpenAIClientConfigBuilder
Use builder syntax to set the inputs and finish with build().
OpenAIModel
A model on the OpenAI Responses or Chat Completions API (OpenAIModel).
OpenAIModelConfig
Configuration of an OpenAIModel (OpenAIModelConfig): the BaseModelConfig fields and the OpenAI ones, for both APIs.
OpenAIModelConfigBuilder
Use builder syntax to set the inputs and finish with build().
OpenAIModelOptions
Options for OpenAIModel::new (OpenAIModelOptions): the API, the OpenAIModelConfig fields and the client ones.
OpenAIModelOptionsBuilder
Use builder syntax to set the inputs and finish with build_options().
SharedCredentialsProvider
Credentials Provider wrapper that may be shared

Enums§

ApiKey
An OpenAI API key: a fixed key or a function called before each request. Strings convert into ApiKey::Key.
OpenAIApi
Which OpenAI API a model streams from (OpenAIApi).