chat-rs
Providers

OpenAI

OpenAI models over the Responses API wire, with native tools, embeddings, multimodal, and input streaming.

OpenAIBuilder presets the OpenAI endpoint and auth, and builds on the Responses API wire for completion and streaming. Hand the built client to ChatBuilder::with_model and the rest of chat-rs works the same as any other provider.

Install

Enable the openai feature (crate chat-openai):

chat-rs = { version = "0.5.1", features = ["openai"] }

By default the client reads your key from the OPENAI_API_KEY environment variable. You can also set it explicitly with with_api_key.

Usage

use chat_rs::{ChatBuilder, ChatOutcome, openai::OpenAIBuilder, parts, types::messages};

let client = OpenAIBuilder::new()
    .with_model("gpt-4o-mini")
    .build();

let mut chat = ChatBuilder::new().with_model(client).build();

let mut messages = messages::from_user(parts!["Hey there!"]);

if let ChatOutcome::Complete(res) = chat.complete(&mut messages).await.map_err(|e| e.err)? {
    println!("{:#?}", res.content);
}

Configuration

The builder methods cover the OpenAI-specific surface:

  • with_model(impl Into<String>) selects the model and moves the builder into its ready-to-build state.
  • with_api_key(String) supplies the key directly instead of reading OPENAI_API_KEY.
  • with_custom_url(String) points the client at a custom OpenAI-compatible endpoint.
  • with_store(bool) controls whether responses are stored server-side.
  • without_previous_response_id() stops the client from chaining requests via the previous response id, so each call sends full context instead.
  • with_reasoning_effort(&str) sets reasoning effort to "low", "medium", or "high" on models that support it.
  • with_description, with_metadata, and with_transport attach metadata or swap in a custom transport.

Native tools (see native tools) are added on the builder:

  • with_web_search(context_size, user_location) enables OpenAI's web search.
  • with_image_generation(tool) enables image generation. This is Beta; the generated bytes arrive back as image file parts.
use chat_rs::openai::{ImageGenerationTool, ImageQuality, ImageSize, OpenAIBuilder};

let client = OpenAIBuilder::new()
    .with_model("gpt-4.1")
    .with_image_generation(ImageGenerationTool {
        size: Some(ImageSize::Square),
        quality: Some(ImageQuality::Auto),
        ..Default::default()
    })
    .build();

For embeddings, switch the builder into embedding mode with with_embeddings():

let client = OpenAIBuilder::new()
    .with_model("text-embedding-3-small")
    .with_embeddings()
    .build();

let mut chat = ChatBuilder::new().with_model(client).with_embeddings().build();
let response = chat.embed(&mut messages).await.map_err(|e| e.err)?;
println!("{}", response.embeddings.dimension);

Capabilities

  • Completion and streaming are both supported.
  • Embeddings and structured output are supported.
  • Multimodal input works by attaching file parts to a user message.
  • Native tools: web search and image generation.
  • Input streaming lets you push text while the model is producing output, built on the Responses API wire. Enable it with ChatBuilder::with_input_stream.

On this page