Skip to content

Messages & Multi-Modal

YosrAI supports rich message types including Text and Images via the Message primitive.

Usage

Simple Text

from yosrai.engine import Message

msg = Message.user("Hello")

Vision (Images)

You can pass images as URLs or Base64 encoded strings. The SDK handles formatting for the specific provider (OpenAI, Anthropic, Google).

# Image from URL
msg = Message.user("What's in this image?", images=["https://example.com/logo.png"])

# Image from Local File
with open("chart.png", "rb") as f:
    import base64
    b64_data = base64.b64encode(f.read()).decode("utf-8")

msg = Message.user("Analyze this chart", images=[b64_data])

API Reference

yosrai.engine.core.messages.Message

Bases: BaseModel

Represents a chat message with a role and content (which can be a list of blocks).

text_content property

Get the text content of the message.

to_anthropic_dict()

Convert to Anthropic API format.

to_google_dict()

Convert to Google Gemini API format.

to_openai_dict()

Convert to OpenAI API format.

user(text, images=[]) classmethod

Factory for User message with optional images.

yosrai.engine.core.messages.ContentBlock

Bases: BaseModel

Represents a single block of content in a message (Text or Image).

ensure_base64()

Ensure image_base64 is populated. If it's a URL, download it.

from_image(source, media_type=None) classmethod

Create an Image block from a URL, local file path, or base64 string.