package ocaml-ai-sdk
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=44e7f6ebef6da2828ec2844dd5b38c8f37284c08e54a29e06f6a8822df6f8e10
sha512=ce31d0868abf28bcaef7b24a0ab09b37dcfc94db3de96b38ff36a03800ae59a19e8009a35314f93191b3c8f178c40f8569630d4389f56ddc2f2cc94fb7bffc3c
Description
Type-safe, provider-agnostic AI model abstraction inspired by 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 4.6+ model catalog |
|
| OpenAI Chat Completions API — streaming SSE, tool calling with strict mode, GPT-4o/o1/o3/o4-mini catalog |
|
| OpenRouter Chat Completions API — model routing, provider preferences, reasoning, web search, usage accounting, prompt caching |
|
| Core SDK — |
|
| Melange bindings for |
Quick start
opam install ocaml-ai-sdkOne-shot generation
open Ai_core
open Ai_provider_anthropic
let () =
Lwt_main.run @@
let model = Anthropic.create_model "claude-sonnet-4-6" in
let%lwt result = Generate_text.generate ~model ~prompt:"Say hello" () in
Lwt_io.printl result.textStreaming
let () =
Lwt_main.run @@
let model = Anthropic.create_model "claude-sonnet-4-6" in
let%lwt result = Stream_text.stream ~model ~prompt:"Tell me a joke" () in
Lwt_stream.iter_s Lwt_io.printl result.text_streamChat server (with UIMessage protocol)
let handler = Server_handler.create ~model ()
(* Serves SSE responses compatible with useChat() from @ai-sdk/react *)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 >= 4.14
- For Melange bindings:
melange>= 4.0.0
Development
make build # Build all libraries
make test # Run test suites
make dev # Watch modeLicense
MIT
Dependencies (14)
-
trace
>= "0.12" -
uuseg
>= "17.0" -
re2
>= "0.16" -
lwt_ppx
>= "5.9" -
ppx_deriving
>= "5.2" -
base64
>= "1.3" -
lwt_ssl
>= "1.2" -
cohttp-lwt-unix
>= "5.3" -
melange-json-native
>= "2.0" -
jsonschema
>= "0.1" -
yojson
>= "2.2" -
lwt
>= "5.9" -
ocaml
>= "4.14" -
dune
>= "3.21"
Dev Dependencies (3)
-
odoc
with-doc -
alcotest
with-test -
cohttp-lwt
with-test
Used by
None
Conflicts
None