RequestMaterializeris the legacy/default implementationStructuredPromptRequestMaterializeris the new implementation built on prompt classes and markdown templates
StructuredOutputRuntime::withRequestMaterializer() without changing the caller-facing StructuredOutput code.
Prompt Hooks
System Message
Set a system-level instruction that frames the entire extraction task.User Prompt
Add an additional prompt that supplements the input messages. This is useful for providing extraction instructions without mixing them into the data.Examples
Provide input/output examples to guide the LLM’s extraction behavior. Few-shot examples are one of the most effective ways to improve extraction accuracy.Combined Usage
All prompt hooks can be used together in a single request, either through the fluent API or thewith() shorthand.
Using Stringable Objects as Prompts
BothwithSystem() and withPrompt() accept string|\Stringable, so you can pass any object that implements Stringable — such as an xprompt Prompt class — directly, without calling ->render() or (string) yourself. The value is cast to string immediately at the boundary.
Cached Context
For applications that use the same large context across multiple requests (such as a long document or a set of reference materials),withCachedContext() marks content for provider-level prompt caching. This can significantly reduce costs and latency when supported by the provider.
StructuredPromptRequestMaterializer path, cached prompt content is no longer flattened into ordinary live messages. It is projected into InferenceRequest::cachedContext() so provider-native caching can take effect, while the per-request prompt remains live.
Switching Materializers
Template Engine Integration
If your application needs a more sophisticated prompt management system with variable interpolation, conditional logic, or template libraries, use the companionTemplate class from the cognesy/template package.
Template class supports Twig and Blade template engines, front matter metadata, chat message markup, and template libraries loaded from disk. Render your templates into strings or message arrays, then pass the result into StructuredOutput. See the Template package documentation for full details.