Basic Request
The simplest way to get a response is to pass a string message directly:Messages::fromString() wraps a plain string as a user message. You can also use
Messages::fromArray() to pass an array of role/content pairs.
The with() Method
When you need to configure multiple request fields at once, use the combined with() method.
It accepts all core request parameters in a single call:
with() are optional — pass only what you need:
Reasoning is configured separately with
withReasoning(). See
Reasoning for model-aware effort, budget, and adaptive
selections.
Focused Helper Methods
For better readability, use the dedicated fluent helpers instead of packing everything into a singlewith() call:
withMessages(...)— set the conversation messageswithModel(...)— override the modelwithTools(...)— attach tool/function definitionswithToolChoice(...)— control tool selectionwithResponseFormat(...)— specify the response formatwithOptions(...)— set provider-specific optionswithStreaming(...)— enable or disable streamingwithMaxTokens(...)— set the maximum token countwithReasoning(...)— set portable, model-validated reasoning intentwithCachedContext(...)— attach reusable cached contextwithRetryPolicy(...)— configure retry behaviorwithResponseCachePolicy(...)— configure response caching
Using the Messages Class
For complex conversations, use the Messages class to build message sequences with
a more expressive API:
The asDeveloper() method maps to OpenAI’s developer role and is automatically
normalized for providers that do not support it.
Message Formats
ThewithMessages() method requires a Messages object. You can create one from
different input formats using the factory methods on Messages:
Messages::fromString($text)— wraps a plain string as a single user messageMessages::fromArray($array)— converts an array of role/content pairsMessagesfluent API — build messages withasSystem(),asUser(),asDeveloper(),asAssistant()
Multimodal Content
For providers that support vision, you can include images in your messages:Using InferenceRequest Directly
If your application already constructs request objects — for example, when deserializing
stored requests or building them in a pipeline — you can pass them in directly:
InferenceRequest objects are immutable value objects. Use with() or the dedicated
mutators (withMessages(), withModel(), etc.) to derive modified copies. Note that
InferenceRequest::with() accepts typed objects (Messages, ToolDefinitions,
ToolChoice, ResponseFormat) rather than primitive arrays or strings: