FlutterFever Studio
Back to packages

Open Responses Flutter package guide

Dart client for the OpenResponses API, offering a type‑safe, provider‑agnostic way to call LLM services from Flutter.

Install command
Copy and run in your Flutter project
flutter pub add open_responses

When to use Open Responses

Integrating large language models (LLMs) into a Flutter application can quickly become a tangled web of HTTP calls, JSON parsing, and provider‑specific quirks. Each vendor—whether OpenAI, Anthropic, Cohere, or a self‑hosted solution—exposes its own authentication scheme, request format, and response payload. Open Responses abstracts that complexity behind a single, well‑typed Dart API that adheres to the OpenResponses specification. By normalising request and response models, the package lets developers focus on the business logic of their app rather than the idiosyncrasies of each LLM service. The library is built on top of `http` and `json_serializable`, ensuring that data structures are generated at compile time, which reduces runtime errors and improves IDE support.

The core of Open Responses is a set of generic request and response classes that map directly to the OpenResponses schema. When you instantiate a client for a specific provider, you simply supply the provider identifier and the required API key. The client then translates your high‑level request—such as a chat message, a text completion, or a semantic embedding—into the provider’s native format, sends the HTTP request, and deserialises the response back into the unified model. This approach makes it trivial to switch providers or to run A/B tests across multiple LLMs without rewriting large portions of your codebase. All models are fully type‑safe, meaning you get compile‑time guarantees about required fields, and the generated `fromJson`/`toJson` methods keep your payloads consistent.

From an architectural standpoint, Open Responses fits neatly into clean‑architecture, MVVM, or any repository‑pattern implementation you prefer. You can inject the client into a data‑source class, expose a stream or `Future` from a repository, and let your state‑management solution (Provider, Riverpod, Bloc, GetX, etc.) consume the results. Because the package does not prescribe a state‑management library, it remains agnostic and can be used in any Flutter app, whether you are building a simple mobile UI or a complex multi‑platform solution. The client also supports cancellation tokens, allowing you to abort long‑running LLM calls when a user navigates away, which is essential for responsive UI design.

Getting started is straightforward. First, add the dependency with `flutter pub add open_responses`. The package requires Dart ≥ 2.19 and Flutter ≥ 3.10, and it works on iOS, Android, web, macOS, Windows, and Linux. After adding the dependency, create an `OpenResponsesClient` by providing the provider name and API key. The client handles token refresh, rate‑limit back‑off, and basic error mapping, but you should still implement retry logic and secure storage for keys in production. Pay attention to the size of the payloads you send; some providers impose strict token limits, and large requests can increase latency and cost. Logging can be toggled via the `debug` flag, which is useful during development but should be disabled in release builds to avoid leaking sensitive data.

For newcomers, a quick prototype can be built in under a minute. Define a simple UI with a text field, call `client.completeText(prompt: userInput)`, and display the generated text. This minimal example demonstrates the package’s ease of use while still showcasing its type safety and provider‑agnostic nature. As your app grows, you can expand to streaming responses, multi‑modal inputs, or batch embeddings without changing the underlying client code. Open Responses is actively maintained, well‑documented, and designed to evolve alongside the OpenResponses specification, making it a reliable foundation for any AI‑enhanced Flutter project.

chatbot
content generation
semantic search
code assistance
translation

Pros

  • provider‑agnostic
  • type‑safe models
  • works on all Flutter platforms
  • easy to swap LLM providers
  • compatible with any state‑management solution

Watch outs

  • requires manual API‑key management
  • no built‑in UI components
  • advanced streaming features need extra handling

Setup notes

Run `flutter pub add open_responses` to add the package. Import with `import 'package:open_responses/open_responses.dart';`. Initialise the client: ```dart final client = OpenResponsesClient( provider: 'openai', apiKey: const String.fromEnvironment('OPENAI_API_KEY'), ); ```

Requires Dart >=2.19 and Flutter >=3.10. Supports iOS, Android, web, macOS, Windows, and Linux. Works with null‑safety enabled projects.

```dart
import 'package:flutter/material.dart';
import 'package:open_responses/open_responses.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Open Responses Demo')),
        body: const PromptWidget(),
      ),
    );
  }
}

class PromptWidget extends StatefulWidget {
  const PromptWidget({super.key});
  @override
  State<PromptWidget> createState() => _PromptWidgetState();
}

class _PromptWidgetState extends State<PromptWidget> {
  final _controller = TextEditingController();
  String _response = '';
  final _client = OpenResponsesClient(
    provider: 'openai',
    apiKey: const String.fromEnvironment('OPENAI_API_KEY'),
  );

  Future<void> _sendPrompt() async {
    final result = await _client.completeText(prompt: _controller.text);
    setState(() => _response = result.output);
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16),
      child: Column(
        children: [
          TextField(controller: _controller, decoration: const InputDecoration(labelText: 'Prompt')),
          const SizedBox(height: 12),
          ElevatedButton(onPressed: _sendPrompt, child: const Text('Generate')),
          const SizedBox(height: 24),
          Text(_response, style: const TextStyle(fontSize: 16)),
        ],
      ),
    );
  }
}
```

Official package resources