🤖lauren-ai
← Home
Export this page

Transport & Multimodal

Core message types exchanged with LLM providers.

Messages & completions

Message

python
class Message(role: Literal['user', 'assistant'], content: str | list[ContentBlock])

A single message in a conversation.

Parameters:

NameTypeDescription
roleLiteral['user', 'assistant']The sender role. Either "user" or "assistant".
contentstr | list[ContentBlock]Message content. Either a plain string or a list of ContentBlock instances for multi-modal / tool-use messages.

Message.user

python
def user(cls, content: str | list[ContentBlock]) -> Message

Convenience factory for a user message.

Parameters:

NameTypeDescription
contentstr | list[ContentBlock]Message content.

Returns: Message — A new user Message.

Message.assistant

python
def assistant(cls, content: str | list[ContentBlock]) -> Message

Convenience factory for an assistant message.

Parameters:

NameTypeDescription
contentstr | list[ContentBlock]Message content.

Returns: Message — A new assistant Message.

Message.from_multimodal

python
def from_multimodal(cls, role: str, parts: list[Any]) -> Message

Create a Message from a list of text strings and content objects.

Accepts any mix of plain strings, ImageContent, AudioContent, and DocumentContent instances.

Parameters:

NameTypeDescription
rolestrThe sender role — "user" or "assistant".
partslist[Any]Mixed list of text strings and multimodal content objects.

Returns: Message — A new Message whose content is the parts list.

Message.text

python
def text(self) -> str

Extract all text from this message as a single concatenated string.

Returns: str — All text content concatenated together.

Completion

python
class Completion(id: str, model: str, content: str, tool_calls: list[ToolCall], stop_reason: Literal['end_turn', 'tool_use', 'max_tokens', 'stop_sequence'], usage: TokenUsage, thinking_blocks: list[ThinkingBlock | RedactedThinkingBlock] = list())

A finished (non-streaming) model completion.

Parameters:

NameTypeDescription
idstrProvider-assigned completion identifier.
modelstrThe model that produced this completion.
contentstrThe primary text response from the model.
tool_callslist[ToolCall]All tool calls requested by the model in this turn.
stop_reasonLiteral['end_turn', 'tool_use', 'max_tokens', 'stop_sequence']Why the model stopped generating:
  • "end_turn" — natural end of response.
  • "tool_use" — one or more tool calls were requested.
  • "max_tokens" — the configured token limit was reached.
  • "stop_sequence" — a stop sequence was hit. | | usage | TokenUsage | Token usage statistics for this completion. | | thinking_blocks | list[ThinkingBlock | RedactedThinkingBlock] | Extended-thinking blocks (Anthropic only). |

CompletionChunk

python
class CompletionChunk(delta: str = '', thinking_delta: str | None = None, tool_call_delta: ToolCallDelta | None = None, stop_reason: str | None = None, usage: TokenUsage | None = None, pending_approval: PendingApproval | None = None, guardrail_override: str | None = None)

A single chunk from a streaming model completion.

Only one of delta, thinking_delta, or tool_call_delta is populated per chunk (though they are not mutually exclusive in the schema).

Parameters:

NameTypeDescription
deltastrText content delta for this chunk.
thinking_deltastr | NoneReasoning / thinking text delta (extended thinking).
tool_call_deltaToolCallDelta | NonePartial tool call update for this chunk.
stop_reasonstr | NoneStop reason, populated only in the final chunk.
usageTokenUsage | NoneToken usage, populated only in the final chunk.
pending_approvalPendingApproval | NoneHuman-in-the-loop pending-approval signal. Present when a tool call requires confirmation before execution.
guardrail_overridestr | NoneWhen set, an output guardrail fired and this string is the replacement content. The runner emits one sentinel chunk with only this field populated (delta="" etc.) after all normal chunks have been yielded. Callers should replace the accumulated streaming text with this value.

Usage & calls

TokenUsage

python
class TokenUsage(input_tokens: int, output_tokens: int, cache_read_tokens: int = 0, cache_write_tokens: int = 0)

Token accounting for a single model call.

Parameters:

NameTypeDescription
input_tokensintNumber of tokens in the prompt / input messages.
output_tokensintNumber of tokens in the completion.
cache_read_tokensintTokens read from the prompt cache (Anthropic).
cache_write_tokensintTokens written to the prompt cache (Anthropic).

TokenUsage.cost_usd

python
def cost_usd(self, model: str) -> float

Estimate the cost in USD for this usage against model.

Uses a bundled price table with prefix-matching. Falls back to $1/$3 per million tokens when the model is unknown.

Parameters:

NameTypeDescription
modelstrModel identifier used for the completion.

Returns: float — Estimated cost in USD.

ToolCall

python
class ToolCall(tool_use_id: str, name: str, input: dict[str, Any])

A completed tool call extracted from a model response.

Parameters:

NameTypeDescription
tool_use_idstrProvider-assigned identifier for this tool call.
namestrThe registered tool name.
inputdict[str, Any]The parsed JSON input arguments.

ToolSchema

python
class ToolSchema(name: str, description: str, input_schema: dict[str, Any])

JSON Schema descriptor for a tool exposed to the model.

Parameters:

NameTypeDescription
namestrThe tool's registered name (snake_case).
descriptionstrHuman-readable description used in the model's system context.
input_schemadict[str, Any]JSON Schema object describing the tool's input parameters.

Embedding

python
class Embedding(index: int, vector: list[float])

A single embedding vector.

Parameters:

NameTypeDescription
indexintZero-based index of this embedding in the batch.
vectorlist[float]The floating-point embedding vector.

Structured output

StructuredLLM

python
class StructuredLLM(llm: Any, model_cls: type[T])

Typed wrapper over LLMService that forces structured output.

Created via llm.with_structured_output(MyModel).

Usage:

python
structured = llm.with_structured_output(SentimentResult)
result: SentimentResult = await structured.complete([...])

StructuredLLM.complete

python
def complete(self, messages: list[Any]) -> T

Complete messages and return a validated model instance.

Uses tool-calling to force the model to emit JSON that matches the schema, then constructs and returns a model_cls instance.

Parameters:

NameTypeDescription
messageslist[Any]Conversation messages.

Returns: T — A validated instance of model_cls.

Raises:

ExceptionDescription
OutputParserErrorWhen the model's response cannot be parsed or validated against the schema.

Multimodal content

ImageContent

python
class ImageContent(_data: bytes | None = None, _url: str | None = None, mime_type: str = 'image/png')

An image content block for multimodal messages.

Usage:

python
img = ImageContent.from_file("/tmp/chart.png")
img = ImageContent.from_url("https://example.com/photo.jpg")
img = ImageContent.from_bytes(b"...", mime_type="image/jpeg")

ImageContent.from_file

python
def from_file(cls, path: str | Path) -> ImageContent

Load image bytes from path and detect MIME type from extension.

Parameters:

NameTypeDescription
pathstr | PathPath to the image file.

Returns: ImageContent — A new ImageContent with bytes loaded.

ImageContent.from_url

python
def from_url(cls, url: str, mime_type: str = 'image/jpeg') -> ImageContent

Create an image referencing a remote URL.

Parameters:

NameTypeDescription
urlstrThe URL of the image.
mime_typestrMIME type hint. Defaults to "image/jpeg".

Returns: ImageContent — A new ImageContent referencing the URL.

ImageContent.from_bytes

python
def from_bytes(cls, data: bytes, mime_type: str) -> ImageContent

Create an image from raw bytes.

Parameters:

NameTypeDescription
databytesRaw image bytes.
mime_typestrMIME type of the image, e.g. "image/png".

Returns: ImageContent — A new ImageContent wrapping the bytes.

ImageContent.from_base64

python
def from_base64(cls, b64: str, mime_type: str) -> ImageContent

Create an image from a base64-encoded string.

Parameters:

NameTypeDescription
b64strBase64-encoded image data.
mime_typestrMIME type of the image.

Returns: ImageContent — A new ImageContent decoded from b64.

ImageContent.to_anthropic_block

python
def to_anthropic_block(self) -> dict[str, Any]

Serialize to Anthropic API image-block format.

Returns: dict[str, Any] — Dictionary suitable for the Anthropic messages API.

ImageContent.to_openai_block

python
def to_openai_block(self) -> dict[str, Any]

Serialize to OpenAI API image-block format.

Returns: dict[str, Any] — Dictionary suitable for the OpenAI messages API.

AudioContent

python
class AudioContent(_data: bytes, mime_type: str)

An audio content block.

Note: Only supported by OpenAI (input_audio). Anthropic and Ollama raise UnsupportedContentError.

AudioContent.from_file

python
def from_file(cls, path: str | Path) -> AudioContent

Load audio bytes from path and detect MIME type from extension.

Parameters:

NameTypeDescription
pathstr | PathPath to the audio file.

Returns: AudioContent — A new AudioContent with bytes loaded.

AudioContent.from_bytes

python
def from_bytes(cls, data: bytes, mime_type: str) -> AudioContent

Create audio from raw bytes.

Parameters:

NameTypeDescription
databytesRaw audio bytes.
mime_typestrMIME type, e.g. "audio/mpeg" or "audio/wav".

Returns: AudioContent — A new AudioContent wrapping the bytes.

AudioContent.to_openai_block

python
def to_openai_block(self) -> dict[str, Any]

Serialize to OpenAI API input_audio block format.

Returns: dict[str, Any] — Dictionary suitable for the OpenAI messages API.

DocumentContent

python
class DocumentContent(_data: bytes | None = None, _url: str | None = None, mime_type: str = 'application/pdf')

A document (PDF) content block.

Anthropic supports native PDF documents. Other providers raise UnsupportedContentError.

DocumentContent.from_file

python
def from_file(cls, path: str | Path) -> DocumentContent

Load document bytes from path.

Parameters:

NameTypeDescription
pathstr | PathPath to the PDF file.

Returns: DocumentContent — A new DocumentContent with bytes loaded.

DocumentContent.from_url

python
def from_url(cls, url: str) -> DocumentContent

Create a document referencing a remote URL.

Parameters:

NameTypeDescription
urlstrThe URL of the document.

Returns: DocumentContent — A new DocumentContent referencing the URL.

DocumentContent.from_bytes

python
def from_bytes(cls, data: bytes, mime_type: str = 'application/pdf') -> DocumentContent

Create a document from raw bytes.

Parameters:

NameTypeDescription
databytesRaw document bytes.
mime_typestrMIME type. Defaults to "application/pdf".

Returns: DocumentContent — A new DocumentContent wrapping the bytes.

DocumentContent.to_anthropic_block

python
def to_anthropic_block(self) -> dict[str, Any]

Serialize to Anthropic API document-block format.

Returns: dict[str, Any] — Dictionary suitable for the Anthropic messages API.