ReqLLM. Provider. Defaults. ResponseBuilder
(ReqLLM v1.14.0)
View Source
Default ResponseBuilder implementation for OpenAI-compatible providers.
This module provides the standard Response assembly logic used by most providers (OpenAI Chat API, xAI, Groq, Cerebras, OpenRouter, etc.).
Provider-specific builders can delegate to this implementation and then apply their own post-processing, or override entirely.
Responsibilities
- Accumulate content from StreamChunks (text, thinking, tool_calls)
- Merge fragmented tool call arguments
- Normalize tool calls to
ToolCallstructs - Build
Messagewith proper content parts - Construct final
Responsestruct with metadata
Usage
This is typically called via ResponseBuilder.for_model/1:
builder = ResponseBuilder.for_model(model)
{:ok, response} = builder.build_response(chunks, metadata, opts)Or directly for OpenAI-compatible providers:
{:ok, response} = Defaults.ResponseBuilder.build_response(chunks, metadata, opts)
Summary
Functions
Build content parts from text, thinking, and tool calls.
Normalize tool calls to ToolCall structs.
Functions
@spec build_content_parts(String.t(), String.t(), [ReqLLM.ToolCall.t()]) :: [ ReqLLM.Message.ContentPart.t() | map() ]
Build content parts from text, thinking, and tool calls.
Special case: if there are no tool calls and text looks like JSON, it may be structured output and is parsed accordingly.
@spec normalize_tool_calls([map() | ReqLLM.ToolCall.t()]) :: [ReqLLM.ToolCall.t()]
Normalize tool calls to ToolCall structs.
Accepts various input formats:
ToolCallstructs (passed through)- Maps with atom keys
%{id:, name:, arguments:} - Maps with string keys
%{"id" =>, "name" =>, "arguments" =>}
Arguments can be maps (encoded to JSON) or JSON strings (passed through).