Skip to main content

Crate nah_chat

Crate nah_chat 

Source
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, &params)
       .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§

ChatClient
The object to hold information about the model server and reqwest HTTP client.
ChatCompletionParamsBuilder
A builder for creating parameters for chat completion requests.
ChatCompletionUsage
Token usage of a chat completion request, reported by the server.
ChatMessage
Data structure of a chat message, could be from the user, the assistant or the tool.
ChatResponseChunkDelta
Chunk delta of chat message from the assistant.
CompletionTokensDetails
Detailed token usage of the completion part.
Error
Error type of nah_chat.
FunctionCallRequest
A function call request.
FunctionCallRequestChunkDelta
A function call request chunk received from stream api.
PromptTokensDetails
Detailed token usage of the prompt part.
ResponseContentPart
A content part of a message item. Supports input_text / output_text / input_image via part_type; extra fields are tolerated.
ResponseFunctionCallItem
A function_call input/output item. The tool format of the Responses API is FLAT: {"type":"function_call","call_id":...,"name":...,"arguments":...}.
ResponseFunctionCallOutputItem
A function_call_output input item carrying the result of a tool call.
ResponseInputTokensDetails
ResponseMessageItem
A message input/output item. Content can be a plain string or a list of content parts.
ResponseObject
The full response object returned by the Responses API (non-stream mode, or carried inside the terminal streaming events).
ResponseOutputItem
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.
ResponseOutputTokensDetails
ResponseUsage
ResponsesParamsBuilder
A builder for creating parameters of Responses API requests.
ToolCallRequest
A tool call request. Only function call is supported now.
ToolCallRequestChunkDelta
A tool call request chunk received from stream api.
TypedChatMessageContent
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§

ChatCompletionStreamEvent
A parsed event from the chat completion streaming interface.
ChatMessageContentValue
Type for message contents.
ChatResponseChunk
A chunk of chat message response from the assistant.
ErrorKind
Error kinds that may occur in nah_chat.
ResponseInputItem
An input item of the Responses API. Unknown shapes are passed through as raw JSON.
ResponseMessageContent
Content of a message item: plain text or typed content parts.
ResponsesInput
Input of a Responses API request: a plain text string, or a list of input items.
ResponsesStreamEvent
A parsed event from the Responses API streaming interface.

Type Aliases§

Result