ToolCallRules

org.llm4s.llmconnect.provider.ToolCallRules
@Stable
object ToolCallRules

The rules every chat client applies to the tool calls of a reply, on complete and on a stream alike (RFC 0009, Part 3):

  1. A call whose arguments are not a complete JSON object is a Left: never {}, never dropped, never passed on as a raw string. Arguments nested more than BoundedJson.MaxDepth levels deep stay unparsed, and fail the same way (#1562).
  2. Absent or blank arguments are {} (a call to a tool with no parameters) only when the reply ended cleanly, with ToolCalls or Stop.
  3. A tool call in a reply cut by the output limit (FinishReason.Length) is a Left: a later call may be missing, or a call's arguments a valid prefix of what the model meant.

The Left is the malformed-tool-call ProcessingError the clients already returned for a call they could not read.

A provider module applies them with checked to a whole reply, parseArguments to a call's argument text and wholeCall to a chunk that carries a whole call; StreamingAccumulator.toCompletion applies them to a stream.

Attributes

Source
ToolCallRules.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def checked(completion: Completion): Result[Completion]

Rule 3, and rule 1 on each call's parsed arguments: a reply with a tool call that was cut by the output limit, or whose arguments are not a JSON object, is a Left; any other reply is unchanged. Reads the calls from Completion.toolCalls, else from the message.

Rule 3, and rule 1 on each call's parsed arguments: a reply with a tool call that was cut by the output limit, or whose arguments are not a JSON object, is a Left; any other reply is unchanged. Reads the calls from Completion.toolCalls, else from the message.

Attributes

Source
ToolCallRules.scala
def parseArguments(name: String, raw: String): Result[Value]

A call's arguments, as the text the provider sent, read as a JSON object (rule 1); blank is {} (rule 2: the caller checks the reply ended cleanly). The error names the call and says why, without the arguments themselves: they are model output, possibly large or deep.

A call's arguments, as the text the provider sent, read as a JSON object (rule 1); blank is {} (rule 2: the caller checks the reply ended cleanly). The error names the call and says why, without the arguments themselves: they are model output, possibly large or deep.

Attributes

Source
ToolCallRules.scala

parseArguments for a call not yet named.

parseArguments for a call not yet named.

Attributes

Source
ToolCallRules.scala

A chunk from a provider that streams each tool call whole (Gemini, Vertex AI, Ollama, watsonx), as the accumulator is to take it: the call's object arguments as their JSON text. The accumulator reads an empty ujson.Obj() as a placeholder, as Anthropic and Bedrock send one before the fragments, so an explicit {} from these providers would otherwise read as no arguments, which rule 2 refuses after any finish but ToolCalls or Stop. The caller's onChunk still gets the chunk unchanged.

A chunk from a provider that streams each tool call whole (Gemini, Vertex AI, Ollama, watsonx), as the accumulator is to take it: the call's object arguments as their JSON text. The accumulator reads an empty ujson.Obj() as a placeholder, as Anthropic and Bedrock send one before the fragments, so an explicit {} from these providers would otherwise read as no arguments, which rule 2 refuses after any finish but ToolCalls or Stop. The caller's onChunk still gets the chunk unchanged.

Attributes

Source
ToolCallRules.scala