Skip to content

nodestep.chat

Chat messages, requests and responses, and the Chat protocol for models.

A chat model is any object with model, complete and stream. See Agents and tools for the chat integrations, and Write a chat integration.

Chat models

Chat

Protocol of a chat model: any object with model, complete and stream.

Attributes
modelstr

Name of the model; middleware sees it as ModelMiddlewareContext.model.

Methods

async complete(request)

Send a request and return the full response.

async complete(request) -> ChatResponse

Parameters
requestChatRequest

Messages, tool definitions and an optional output schema.

Returns
ChatResponse

The model's reply.

stream(request)

Send a request and yield the response as it is generated.

stream(request) -> AsyncIterator[ChatStreamChunk]

Parameters
requestChatRequest

Messages, tool definitions and an optional output schema.

Returns
AsyncIterator[ChatStreamChunk]

Content, tool-call, finish and usage chunks in arrival order.

Requests and responses

ChatRequest

ChatRequest(*, messages, tools=[], tool_choice=None, output_schema=None)

Request to a chat model, the same for every chat integration.

Attributes
messageslist[Message]
toolslist[ToolDefinition]
tool_choiceLiteral['auto', 'required', 'none'] | str | None

Whether the model may, must or must not call tools; another string names the tool to call, and None sends no choice.

output_schemadict[str, Any] | None

JSON schema the answer must follow.

ChatResponse

ChatResponse(*, content=None, refusal=None, tool_calls=[], finish_reason=None, usage={}, model=None, raw=None)

Reply from a chat model, the same for every chat integration.

Attributes
contentstr | None

Answer text; None when the model only calls tools or refuses.

refusalstr | None

The model's refusal text, when it declined to answer.

tool_callslist[ToolCall]
finish_reasonstr | None

Why the model stopped, as the chat integration reports it.

usagedict[str, Any]

Token counts as the chat integration reports them.

modelstr | None

Model that answered, as the chat integration names it.

rawAny

The response object the chat integration received, for fields nodestep does not map.

ChatStreamChunk

ChatStreamChunk(*, content_delta=None, refusal_delta=None, tool_call_delta=None, finish_reason=None, usage=None, model=None, raw=None)

One incremental piece of a streamed response.

Attributes
content_deltastr | None

Next part of the answer text.

refusal_deltastr | None

Next part of the refusal text.

tool_call_deltadict[str, Any] | None

Part of one tool call, shaped as in Streamed tool calls.

finish_reasonstr | None

Why the model stopped; set on a final chunk.

usagedict[str, Any] | None

Token counts; set on the chunk that reports them.

modelstr | None

Model that answered, as the chat integration names it.

rawAny

The event object the chat integration received.

assemble_stream

async assemble_stream(chunks) -> ChatResponse

Collect streamed chunks into a single response.

Parameters
chunksAsyncIterator[ChatStreamChunk]

Chunks produced by Chat.stream.

Returns
ChatResponse

Joined content, assembled tool calls, finish reason, usage and the model named by the chunks.

Raises
ModelProviderError

If a streamed tool call has no id or no name.

Messages

Message

Message: SystemMessage or HumanMessage or AIMessage or ToolMessage

Any chat message, told apart by its type field when validated.

BaseMessage

BaseMessage(*, id=None, content=None)

Base class of all chat messages.

Attributes
idstr | None

Optional message id, for example to remove the message later.

contentstr | None
rolestr

Role of the message as a chat integration sends it.

SystemMessage

SystemMessage(*, id=None, content=None, type='system')

Instruction message for the model.

Subclass of BaseMessage.

Attributes
typeLiteral['system']
rolestr

Always "system".

HumanMessage

HumanMessage(*, id=None, content=None, type='human')

Message written by the user.

Subclass of BaseMessage.

Attributes
typeLiteral['human']
rolestr

Always "user".

AIMessage

AIMessage(*, id=None, content=None, type='ai', refusal=None, tool_calls=[], finish_reason=None, usage={})

Reply produced by the model: text, a refusal, or tool calls.

Subclass of BaseMessage.

Attributes
typeLiteral['ai']
refusalstr | None

The model's refusal text, when it declined to answer.

tool_callslist[ToolCall]
finish_reasonstr | None

Why the model stopped, as the chat integration reports it.

usagedict[str, Any]

Token counts as the chat integration reports them.

rolestr

Always "assistant".

ToolMessage

ToolMessage(*, id=None, content=None, type='tool', name=None, tool_call_id=None, arguments=None)

Result of a tool call; arguments are the ones it ran with when they differ from the call.

Subclass of BaseMessage.

Attributes
typeLiteral['tool']
namestr | None

Name of the tool that ran.

tool_call_idstr | None

Id of the ToolCall this message answers.

argumentsdict[str, Any] | None
rolestr

Always "tool".

Tool calls

ToolCall

ToolCall(*, id, name, arguments={}, arguments_error=None)

A tool invocation requested by the model.

Attributes
idstr

Id the model gave the call; its ToolMessage answers with it.

namestr
argumentsdict[str, Any]

Parsed arguments; empty when arguments_error is set.

arguments_errorstr | None

Why the argument text could not be parsed, or None.

arguments_jsonstr

Arguments serialized as a JSON string.

Methods

from_arguments_json(id, name, arguments_json)

class method Build a tool call from the JSON argument text a model produced.

from_arguments_json(id, name, arguments_json) -> ToolCall

Parameters
idstr

Tool call id.

namestr

Tool name.

arguments_jsonstr | None

Argument text, parsed with parse_tool_arguments.

Returns
ToolCall

The call, with arguments_error set when the text is invalid.

ToolDefinition

ToolDefinition(*, name, description, input_schema={})

Tool description sent to the model.

Attributes
namestr
descriptionstr
input_schemadict[str, Any]

JSON schema of the arguments.

parse_tool_arguments

parse_tool_arguments(raw) -> tuple[dict[str, Any], str | None]

Parse the JSON arguments of a tool call.

Parameters
rawstr | None

Argument text produced by the model.

Returns
tuple[dict, str or None]

The parsed arguments and an error description when they are invalid. Empty text means no arguments; anything but a JSON object, null included, is invalid.