package ai-sdk-react
All checked builds passInstall
dune-project
Dependency
Authors
Maintainers
Sources
sha256=5f27653745461a43a89095b73b02a2d4db951410abfd79203af4b5429b9a4109
sha512=f07e57ab33aed342eb5d1282a5d4c398b13e4f8ce4fdcf6fef4c0fbe78146a2e0ecfc220294fa4b4767c9d3c6deba8420eed23e5fec248d34c05cf4b7be72ed5
Description
Type-safe OCaml/Melange bindings for Vercel AI SDK)
Added to opam-repository:
README
ocaml-ai-sdk
Type-safe, provider-agnostic AI model abstraction for OCaml, inspired by the Vercel AI SDK. Targets wire compatibility with AI SDK v6 so you can pair an OCaml backend with @ai-sdk/react frontends.
Libraries
Library | opam lib | Description |
|---|---|---|
|
| Provider abstraction — language model module types, tool definitions, prompt types, GADT-based provider options |
|
| Anthropic Messages API — streaming SSE, thinking, cache control, Claude model catalog |
|
| OpenAI Chat Completions API — streaming SSE, tool calling with strict mode, GPT-4o to GPT-6.1 and o-series catalog |
|
| OpenRouter Chat Completions API — model routing, provider preferences, reasoning, web search, usage accounting, prompt caching |
|
| Core SDK — |
|
| Mock language model for tests, including models that hang until aborted |
|
| Melange bindings for |
Quick start
opam install ocaml-ai-sdkOne-shot generation
let () =
Lwt_main.run @@
let model = Ai_provider_anthropic.model "claude-sonnet-5-5" in
let%lwt result = Ai_core.Generate_text.generate_text ~model ~prompt:"Say hello" () in
Lwt_io.printl result.textStreaming
let () =
Lwt_main.run @@
let model = Ai_provider_anthropic.model "claude-sonnet-5-5" in
let result = Ai_core.Stream_text.stream_text ~model ~prompt:"Tell me a joke" () in
Lwt_stream.iter_s Lwt_io.print result.text_streamChat server (with UIMessage protocol)
Server_handler.handle_chat turns a useChat() request body into an SSE response. The response is a plain record (status, headers, body stream), so it works with any web server. With cohttp:
let respond (r : Ai_core.Server_handler.response) =
Cohttp_lwt_unix.Server.respond ~status:(Cohttp.Code.status_of_code r.status)
~headers:(Cohttp.Header.of_list r.headers) ~body:(Cohttp_lwt.Body.of_stream r.body) ()
let callback _conn _req body =
let%lwt body = Cohttp_lwt.Body.to_string body in
respond (Ai_core.Server_handler.handle_chat ~model body)To inspect or change the request before generating, parse it with Server_handler.parse_chat_request and call Stream_text.stream_text yourself.
Cancellation
Pass an Ai_provider.Abort_signal.t to stop a call. Tools get the same signal in their execute options, and on_abort receives the steps recorded before the abort, so their usage can still be counted:
let say_hello ~model =
Ai_provider.Abort_signal.with_timeout 30. (fun abort_signal ->
Ai_core.Generate_text.generate_text ~model ~prompt:"Say hello" ~abort_signal ())See the cancellation guide for Lwt.cancel, combining signals, stream_text aborts and stopping generation when a web client disconnects.
See examples/ for complete runnable demos including tool use, thinking, structured output, OpenRouter prompt caching, and full-stack Melange apps.
Architecture
ai_provider Provider abstraction (module types, GADT options)
├── ai_provider_anthropic Anthropic implementation
├── ai_provider_openai OpenAI implementation
├── ai_provider_openrouter OpenRouter implementation
└── ai_core Core SDK (generate, stream, UIMessage protocol)Key design choices:
- Provider options use an extensible GADT (
type _ key = ..) for compile-time type-safe provider-specific settings (e.g. thinking budget, cache control, OpenRouter routing) - Prompt types are role-constrained variants —
Systemaccepts only strings,Useraccepts text + files, etc. - Streaming uses
Lwt_stream.t—stream_textreturns synchronously with streams populated by a background Lwt task - UIMessage protocol emits SSE chunks matching the
ai@6Zod schemas exactly, souseChat()works without adaptation
Anthropic thinking
Anthropic_options.thinking = None omits the thinking request field. Use Some Thinking.Disabled to explicitly disable thinking on models that default to it; Adaptive and Enabled select the corresponding Anthropic modes. effort is independent and is serialized under output_config.
When forwarding Anthropic reasoning blocks, preserve their provider metadata signature. Omitted-display thinking can have empty text, but its signature is required for multi-turn and tool-loop resubmission.
AI SDK v6 compatibility
The UIMessage stream protocol (x-vercel-ai-ui-message-stream: v1) is wire-compatible with:
@ai-sdk/react3.x (useChat,useCompletion)ai6.x (core SDK)
All chunk types are supported: text, reasoning, tool call (start/delta/result), source, file, data, error, finish message/step.
Requirements
- OCaml >= 5.4
- libcurl (the system package; opam installs it through
conf-libcurl) - For Melange bindings:
melange>= 4.0.0
Development
make build # Build all libraries
make test # Run test suites
make dev # Watch modeLicense
MIT
Dev Dependencies (1)
-
odoc
with-doc
Optional Dependencies
None
Used by
None
Conflicts
None
Build status
Results of building ai-sdk-react with each compiler on check.ci.ocaml.org, run of against opam-repository 8d8d144. All results for this package.
Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.
| Version | OCaml 4.11 | OCaml 4.14 | OCaml 5.2 | OCaml 5.4 | OCaml 5.5 |
|---|---|---|---|---|---|
| 0.9 (this version) | n/a | n/a | n/a | good | good |
| 0.6.3 | n/a | good | good | good | good |
- good Package successfully built
- partial One of the dependencies failed to build
- bad Package failed to build
- n/a Package is not available in this environment
- internal Internal failure