Skip to main content
Instructor builds a structured prompt from several components: system text, user messages, a mode-specific instruction prompt, examples, and retry context. You can customize most of these to tune extraction behavior without changing the underlying extraction flow. There are currently two prompt materializers in the package:
  • RequestMaterializer is the legacy/default path
  • StructuredPromptRequestMaterializer is the new path using prompt classes and markdown templates
Both can be selected through StructuredOutputRuntime::withRequestMaterializer().

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 new structured prompt path it is rendered inside the single system prompt body alongside the mode-specific extraction instructions.
You can also pass both through the with() method:

Examples

Few-shot examples are another prompt component. On the new structured prompt path they are rendered as markdown inside the system prompt to demonstrate the expected extraction style:
See the Demonstrations page for details on the 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. Use withCachedContext() to mark content as cacheable:
The cached context is placed before the per-request content in the prompt. On the new structured prompt path, cached system text, cached task text, and cached examples are rendered into a cached system prompt and projected through provider-native cached context. Content passed through withCachedContext() is marked with cache control headers where the provider supports them.

Mode-Specific Prompts

Instructor uses a default prompt for each output mode that tells the model how to format its response. On the legacy path these prompts are inline strings. On the new path they are prompt classes backed by markdown templates and configured in StructuredOutputConfig.

Overriding Mode Prompts

Legacy inline prompt override:
New prompt-class override:
If you store these in YAML, use FQN strings:

Template Placeholders

Mode prompts support the <|json_schema|> placeholder, which Instructor replaces with the JSON Schema generated from your response model. This is particularly important for Json and MdJson modes, where the schema must be embedded in the prompt:

Tool Name And Description

In OutputMode::Tools, the tool definition sent to the model includes a name and description. These provide semantic context that can improve extraction quality:
The defaults are 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::Json and OutputMode::MdJson ignore tool name and description since they do not use tool calling.

Retry Prompt

When validation fails and retries are enabled, Instructor appends a retry prompt to the conversation. The default is:
Legacy inline retry prompt override:
New prompt-class override:
The same pattern applies to deserialization repair via deserializationErrorPromptClass.

Chat Structure

Instructor assembles the final prompt from named sections in a specific order. The default structure includes sections for system messages, cached context, prompt, examples, messages, and retries. You can reorder or extend this through StructuredOutputConfig:
Most applications will never need to modify the chat structure. It is exposed for advanced use cases where you need precise control over prompt ordering.