Text Generation
Generate text with callModel using various input formats and model configurations. Supports multiple consumption patterns including text, streaming, and structured output.
The simplest way to generate text:
callModel accepts several input formats to match your use case.
The simplest format - a single string becomes a user message:
For multi-turn conversations, pass an array of messages:
For rich content including images:
Set the model’s behavior with the instructions parameter:
Specify a model by its OpenRouter ID:
Provide multiple models for automatic fallback:
The SDK will try each model in order until one succeeds.
Returns just the text content after tool execution completes:
Returns the full response object including usage data:
Control the generation behavior:
Request structured output:
Handle common error cases:
Each callModel invocation is independent: