Expand description
§Introduction
This crate exposes an async stream API for the widely-used OpenAI chat completion API and the Responses API.
Supported features:
- Stream generation
- Tool calls
- Reasoning content (Qwen3, Deepseek R1, etc)
- Token usage in the chat completion stream (via
stream_options.include_usage) - Responses API (stream + non-stream, tool calls, reasoning)
This crate is built on top of tokio, reqwest and serde_json.
use nah_chat::{ChatClient, ChatCompletionStreamEvent, ChatMessage};
use futures_util::{pin_mut, StreamExt};
let chat_client = ChatClient::init(base_url, auth_token);
// create and pin the stream
let stream = chat_client
.chat_completion_stream(model_name, &messages, ¶ms)
.await
.unwrap();
pin_mut!(stream);
// buffer for the new message
let mut message = ChatMessage::new();
// consume the stream
while let Some(event_result) = stream.next().await {
match event_result {
Ok(ChatCompletionStreamEvent::Delta(delta)) => {
message.apply_model_response_chunk(delta);
}
Ok(ChatCompletionStreamEvent::Usage(usage)) => {
// Optional: the final chunk carries the authoritative token usage.
eprintln!("Usage: {} prompt + {} completion tokens",
usage.prompt_tokens.unwrap_or(0), usage.completion_tokens.unwrap_or(0));
}
Err(e) => {
eprintln!("Error occurred while processing the chat completion: {}", e);
}
}
}§Notice
Copyright 2025, Mengxiao Lin.
This is a part of nah project. nah means “Not A
Human”. Source code is available under MPL-2.0.
Structs§
- Chat
Client - The object to hold information about the model server and
reqwestHTTP client. - Chat
Completion Params Builder - A builder for creating parameters for chat completion requests.
- Chat
Completion Usage - Token usage of a chat completion request, reported by the server.
- Chat
Message - Data structure of a chat message, could be from the user, the assistant or the tool.
- Chat
Response Chunk Delta - Chunk delta of chat message from the assistant.
- Completion
Tokens Details - Detailed token usage of the completion part.
- Error
- Error type of
nah_chat. - Function
Call Request - A function call request.
- Function
Call Request Chunk Delta - A function call request chunk received from stream api.
- Prompt
Tokens Details - Detailed token usage of the prompt part.
- Response
Content Part - A content part of a message item. Supports
input_text/output_text/input_imageviapart_type; extra fields are tolerated. - Response
Function Call Item - A
function_callinput/output item. The tool format of the Responses API is FLAT:{"type":"function_call","call_id":...,"name":...,"arguments":...}. - Response
Function Call Output Item - A
function_call_outputinput item carrying the result of a tool call. - Response
Input Tokens Details - Response
Message Item - A message input/output item. Content can be a plain string or a list of content parts.
- Response
Object - The full response object returned by the Responses API (non-stream mode, or carried inside the terminal streaming events).
- Response
Output Item - An output item of a response. Kept as a tolerant struct (all fields optional except
item_type) because DeepSeek’s compatibility with the OpenAI structure is partial. - Response
Output Tokens Details - Response
Usage - Responses
Params Builder - A builder for creating parameters of Responses API requests.
- Tool
Call Request - A tool call request. Only function call is supported now.
- Tool
Call Request Chunk Delta - A tool call request chunk received from stream api.
- Typed
Chat Message Content - This is used to represent a message content that can have multiple types with a type annotation. Currently, it supports text and image_url.
- URLObject
- This is used to represent a URL in the message content. Currently, only images will be represented in this format.
Enums§
- Chat
Completion Stream Event - A parsed event from the chat completion streaming interface.
- Chat
Message Content Value - Type for message contents.
- Chat
Response Chunk - A chunk of chat message response from the assistant.
- Error
Kind - Error kinds that may occur in
nah_chat. - Response
Input Item - An input item of the Responses API. Unknown shapes are passed through as raw JSON.
- Response
Message Content - Content of a message item: plain text or typed content parts.
- Responses
Input - Input of a Responses API request: a plain text string, or a list of input items.
- Responses
Stream Event - A parsed event from the Responses API streaming interface.