Skip to main content

Instructor Package Cheatsheet

Code-verified quick reference for packages/instructor.

Core Flow

Create StructuredOutput

Request Configuration (StructuredOutput)

Single-call variant:

Runtime / Provider Setup

RequestMaterializer remains the legacy/default implementation during rollout. StructuredPromptRequestMaterializer is the new path: it renders one system prompt text, keeps examples inside that system prompt, and sends cached prompt content through InferenceRequest::cachedContext() when used with the current runtime.

Prompt Config References

YAML-safe shape uses FQN strings:

OutputMode Enum

Pipeline Overrides

Execution

PendingStructuredOutput is a lazy handle:
  • no provider call happens until one of the read methods above is used
  • get(), response(), inferenceResponse(), and stream() coordinate one execution
  • mutable lifecycle bookkeeping sits behind the internal execution session, not on the facade-facing handle
  • long-lived streaming state stays in the dedicated stream/state objects
Type helpers (available on StructuredOutput and PendingStructuredOutput):
Additional type helper (only on PendingStructuredOutput):

Streaming (StructuredOutputStream)

lastResponse() / finalResponse() return StructuredOutputResponse. Use ->inferenceResponse() when you need the nested raw InferenceResponse.

Response Model Helpers

Sequence

Scalar

Maybe

Output Controls

Cached Context

Examples API

Events

Testing

Deterministic test seams:
  • Tests\Support\FakeInferenceDriver
    • queue sync InferenceResponse fixtures or streaming PartialInferenceDelta batches
    • best for most unit and regression tests inside packages/instructor
  • Tests\MockHttp
    • builds an HTTP client around MockHttpDriver
    • use when provider adapter and HTTP response shape still matter
  • Tests\Integration\Support\ProbeStreamDriver
    • observation helper for streaming immediacy and call-count assertions
  • Tests\Support\ProbeIterator
    • explicit iterator helper for controlled delta emission in integration tests