ai_stream_client

A small, typed Dart client for streaming responses from OpenAI-compatible chat APIs. It is provider-neutral, testable, and works in Flutter, Dart servers, and web applications.

Features

  • Parses Server-Sent Events (SSE) into typed chunks
  • Works with OpenAI-compatible gateways and backend proxies
  • Supports custom headers, cancellation signals, and structured errors
  • Includes a convenience API that combines a stream into one response
  • Has no Flutter dependency

Installation

dependencies:
  ai_stream_client: ^0.1.0

Usage

final client = AiStreamClient(
  baseUrl: Uri.parse('https://your-backend.example.com/v1/'),
);

final request = AiChatRequest(
  model: 'your-model',
  messages: const [AiMessage.user('What makes Flutter fast?')],
);

await for (final chunk in client.streamChat(request)) {
  print(chunk.text);
}

client.close();

Use complete when incremental updates are not needed:

final response = await client.complete(request);
print(response.text);

Cancellation and errors

Pass an AiCancelToken and call cancel() when the user stops generation. HTTP failures use AiHttpException; malformed events use AiProtocolException.

Security

Never embed a production provider API key in a mobile or web application. Point this client at a backend you control and keep the provider credential on the server.

Compatibility

The client sends POST chat/completions relative to baseUrl and reads the common data: {...} SSE format. Use request metadata and client defaultHeaders for provider-specific values.

License

MIT © 2026 Syed Waleed Nawaz

Libraries

ai_stream_client
A provider-neutral client for OpenAI-compatible streaming AI APIs.