LlmCall.JSON behaviour (llm_call v0.1.0)

Copy Markdown View Source

The JSON seam.

Config.json is a module. The default is Elixir's built-in JSON, and any module exposing either the tagged or the bang API can be configured:

The facade functions encode/2 and decode/2 dispatch on what the module exports and normalize the result to a tagged tuple, so a codec never raises on a malformed payload. Configure the module at compile time with config :llm_call, json_library: Torque, or per client with json_library:.

A few wire fields are themselves JSON strings: tool-call arguments on OpenAI Chat, and streamed function calls on Gemini. A codec therefore needs a JSON implementation, and the caller may want a different one (Jason, a stricter encoder, a wrapped implementation with metrics).

Summary

Callbacks

Decodes a JSON binary.

The tagged adapter used by LlmCall.JSON itself, backed by Elixir JSON.

Functions

Decodes a binary with the configured module.

Encodes a term with the configured module.

Callbacks

decode(t)

@callback decode(String.t()) :: {:ok, term()} | {:error, term()}

Decodes a JSON binary.

encode(term)

@callback encode(term()) :: {:ok, String.t()} | {:error, term()}

The tagged adapter used by LlmCall.JSON itself, backed by Elixir JSON.

Functions

decode(binary)

decode(module, binary)

@spec decode(module(), String.t()) :: {:ok, term()} | {:error, term()}

Decodes a binary with the configured module.

encode(term)

encode(module, term)

@spec encode(module(), term()) :: {:ok, String.t()} | {:error, term()}

Encodes a term with the configured module.