ReqLLM.Provider.Defaults.ResponseBuilder (ReqLLM v1.13.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

  1. Accumulate content from StreamChunks (text, thinking, tool_calls)
  2. Merge fragmented tool call arguments
  3. Normalize tool calls to ToolCall structs
  4. Build Message with proper content parts
  5. Construct final Response struct 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

build_content_parts(text_content, thinking_content, tool_calls)

@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.

normalize_tool_calls(tool_calls)

@spec normalize_tool_calls([map() | ReqLLM.ToolCall.t()]) :: [ReqLLM.ToolCall.t()]

Normalize tool calls to ToolCall structs.

Accepts various input formats:

  • ToolCall structs (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).