Agentix.Provider.ReqLLM (Agentix v0.2.0)

Copy Markdown View Source

The default Agentix.Provider — a thin wrapper over ReqLLM.stream_text/3, or ReqLLM.stream_object/4 when the turn requests structured output.

When opts carry a :schema (a NimbleOptions keyword or JSON Schema map), the adapter calls ReqLLM.stream_object/4 instead — ReqLLM implements structured output as a forced structured_output tool call. finalize then extracts the parsed object with ReqLLM.Response.unwrap_object/1 (from the already-collected chunks — never re-consuming the stream) and stashes it in the assistant message's metadata["object"], where Agentix.object/1 reads it.

ReqLLM.stream_text/3 returns {:ok, %ReqLLM.StreamResponse{}} (not a bare stream). The struct carries a lazy stream of %ReqLLM.StreamChunk{}, a metadata_handle collected concurrently, and a cancel closure that actually closes the socket. This adapter maps that onto the normalized Agentix.Provider.Stream the agent consumes:

  • chunks — the lazy stream, wrapped so each chunk is accumulated as the agent forwards it as a live delta;
  • cancelStreamResponse's own closure, passed through unchanged (the agent invokes it on :cancel so the HTTP connection is freed);
  • finalize — awaits the metadata handle and rebuilds the finalized assistant %ReqLLM.Message{} + usage from the accumulated chunks, reusing ReqLLM's own ResponseBuilder so tool-call ids (assembled by ReqLLM, absent from individual StreamChunks) survive. Call it after chunks is consumed.

Streaming pools are HTTP/1-only by default (Finch ALPN bug with mixed-protocol large bodies).