Skip to content
Reference

Models and message types

The Model protocol, the message and streaming types, and the helpers that join and split deltas.

On this page

agentling.models defines what a model is and the types that pass between the loop and a model adapter. Agentling ships no adapter; see Write a model adapter and The model contract for the rules an adapter follows.

Model#

class Model(Protocol):
    def stream(
        self,
        messages: Sequence[ChatMessage],
        tools: Sequence[ToolDefinition] = (),
    ) -> AsyncIterator[Delta]: ...

Any object with this method is a model; write it as an async def that yields. Agent() raises TypeError for an object without a callable stream, or one whose stream is a coroutine function rather than an async generator.

ChatMessage#

ChatMessage(role, content="", tool_calls=[], tool_call_id=None, usage=None,
            reasoning=None, finish_reason=None, provider_data=None)
FieldDefaultMeaning
rolerequired"system", "user", "assistant" or "tool" (the Role type).
content""The text.
tool_calls[]ToolCalls an assistant turn asked for.
tool_call_idNoneOn a "tool" message, the id of the call it answers.
usageNoneToken counts for an assistant turn.
reasoningNoneReasoning text the model showed, kept apart from the answer.
finish_reasonNoneWhy the turn stopped.
provider_dataNoneAdapter-owned data, keyed by adapter name, sent back on later turns. Must be JSON-serialisable.

ToolCall#

ToolCall(id: str, name: str, arguments: dict[str, Any]): a tool call the model asked for, with its arguments parsed.

ToolDefinition#

ToolDefinition(name: str, description: str, parameters: dict[str, Any]), frozen: what a model is told about one tool. parameters is a JSON Schema object. Tool.definition returns one, and each adapter renders it in its backend's format.

Delta#

Delta(content=None, tool_calls=[], usage=None, reasoning=None,
      finish_reason=None, provider_data=None)

One streamed chunk of a model turn. Every field is optional, but a Delta should carry at least one.

FieldMeaning
contentA piece of answer text.
tool_callsToolCallDelta fragments.
usageToken counts. The last one in a turn wins.
reasoningA piece of reasoning text.
finish_reasonWhy the turn stopped. Yield it once.
provider_dataAdapter data to store with the turn. The last one wins.

ToolCallDelta#

ToolCallDelta(index: int, id: str | None = None, name: str | None = None, arguments: str | None = None): a piece of a streamed tool call. Pieces with the same index belong to one call; arguments pieces are joined into JSON. A missing id is made up when the turn is joined.

Usage#

Usage(input_tokens: int, output_tokens: int, cached_input_tokens: int = 0, reasoning_tokens: int = 0), frozen. cached_input_tokens and reasoning_tokens are breakdowns already included in the two totals. total_tokens is input_tokens + output_tokens, and + adds two usages field by field.

FinishReason#

Literal["stop", "tool_calls", "length", "content_filter", "error", "unknown"]. Adapters map their backend's stop reasons onto these and use "unknown" for the rest. "length" on a turn whose tool call can't be parsed makes the loop raise ModelOutputTruncatedError.

agglomerate_deltas#

agglomerate_deltas(deltas: Iterable[Delta]) -> ChatMessage

Joins a turn's deltas into one assistant ChatMessage. Content, reasoning and each tool call's arguments are concatenated; for usage, finish_reason and provider_data the last one wins. Tool calls are ordered by index, and a call with no id gets agentling_call_<index>_<8 hex characters>.

Raises ModelOutputError when a tool call has no name, or its arguments aren't valid JSON or aren't a JSON object.

deltas_from_message#

async deltas_from_message(message: ChatMessage) -> AsyncIterator[Delta]

Yields a whole message as one Delta, for a backend that can't stream. Inside stream(): async for delta in deltas_from_message(reply): yield delta. See custom_model_generate_only.py.

Example#

from agentling import ChatMessage, Delta, ToolCallDelta, Usage, agglomerate_deltas
from agentling import deltas_from_message
 
deltas = [
    Delta(content="Let me "),
    Delta(content="check."),
    Delta(tool_calls=[ToolCallDelta(0, None, "lookup", '{"city": ')]),
    Delta(tool_calls=[ToolCallDelta(0, arguments='"Paris"}')]),
    Delta(usage=Usage(12, 7), finish_reason="tool_calls"),
]
message = agglomerate_deltas(deltas)
call = message.tool_calls[0]
 
print(message.content)
print(call.name, call.arguments, call.id.startswith("agentling_call_0_"))
print(message.usage.total_tokens, message.finish_reason)
 
reply = ChatMessage("assistant", "Done.", finish_reason="stop")
print([delta async for delta in deltas_from_message(reply)][0].content)

Output:

Let me check.
lookup {'city': 'Paris'} True
19 tool_calls
Done.
Was this page helpful?