StructuredPromptRequestMaterializer uses prompt classes backed by bundled
Twig templates. Customize it by supplying your own prompt classes.
System And Prompt Text
The two most common customization points are the system message and the prompt text:- System text sets the model’s persona and overall behavior. Use it for stable instructions that apply across many requests.
- Prompt text provides task-specific instructions for this particular extraction. On the default structured prompt path it is rendered inside the single system prompt body alongside the mode-specific extraction instructions.
with() method:
Examples
Few-shot examples are another prompt component. On the default structured prompt path they are rendered as markdown inside the system prompt to demonstrate the expected extraction style:Example class.
Cached Context
Some providers (notably Anthropic) support prompt caching, where stable parts of the conversation are cached between requests to reduce latency and cost. UsewithCachedContext() to mark content as cacheable:
withCachedContext() is marked with cache control headers where the
provider supports them.
Mode-Specific Prompts
Instructor uses a default prompt class for each output mode that tells the model how to format its response. The bundled classes use.md.twig templates and are configured in
StructuredOutputConfig.
Overriding Mode Prompts
Use prompt classes as the supported customization seam:Tool Name And Description
InOutputMode::Tools, the tool definition sent to the model includes a name and
description. These provide semantic context that can improve extraction quality:
extracted_data and Function call based on user instructions.
respectively. Overriding them with task-specific values can help the model understand
what the tool represents.
OutputMode::JsonandOutputMode::MdJsonignore tool name and description since they do not use tool calling.
Retry Prompt
When validation fails and retries are enabled, Instructor renders the configured retry prompt class:deserializationErrorPromptClass.
Legacy Compatibility
The inlinemodePrompts, retryPrompt, and chatStructure settings were removed in 2.7
along with the legacy materializer that read them. StructuredOutputConfig::fromArray()
ignores them rather than failing, so old config files keep loading. Use prompt classes, or
provide a custom CanMaterializeRequest implementation when prompt-class customization is
insufficient.