Overview
Instructor dispatches events at every significant stage of its execution. You can listen to these events for logging, monitoring, debugging, or custom processing. All event classes extendCognesy\Events\Event.
Listening to Events
Targeted Listeners
UseonEvent() on the StructuredOutputRuntime to listen for a specific event type:
Wiretap (All Events)
Usewiretap() to receive every event dispatched by Instructor. This is useful for
debugging or comprehensive logging:
Practical Example
Event Categories
Events are organized into namespaces that correspond to the processing stage:High-Level Events (Events\StructuredOutput)
| Event | When |
|---|---|
StructuredOutputStarted | A structured output operation begins |
StructuredOutputRequestReceived | The request has been received by the runtime |
StructuredOutputResponseGenerated | The final StructuredOutputResponse has been produced |
StructuredOutputResponseUpdated | A streaming partial response is emitted |
Request Events (Events\Request)
| Event | When |
|---|---|
ResponseModelRequested | A response model has been submitted for processing |
ResponseModelBuildModeSelected | The factory has chosen a build strategy |
ResponseModelBuilt | The response model and schema are ready |
NewValidationRecoveryAttempt | A retry attempt is about to begin |
StructuredOutputRecoveryLimitReached | All retries have been exhausted |
SequenceUpdated | A sequence item has been completed during streaming |
Response Events (Events\Response)
| Event | When |
|---|---|
ResponseDeserializationAttempt | Deserialization is about to start |
ResponseDeserialized | Deserialization succeeded |
ResponseDeserializationFailed | Deserialization failed |
CustomResponseDeserializationAttempt | A CanDeserializeSelf implementation is being used |
ResponseValidationAttempt | Validation is about to start |
ResponseValidated | Validation passed |
ResponseValidationFailed | Validation failed |
CustomResponseValidationAttempt | A CanValidateSelf implementation is being used |
ResponseTransformationAttempt | Transformation is about to start |
ResponseTransformed | Transformation succeeded |
ResponseTransformationFailed | Transformation failed |
ResponseConvertedToObject | The final object has been produced |
ResponseGenerationFailed | The entire response generation pipeline failed |
Extraction Events (Events\Extraction)
| Event | When |
|---|---|
ExtractionStarted | Data extraction from the inference response begins |
ExtractionCompleted | Extraction succeeded |
ExtractionFailed | Extraction failed |
ExtractionStrategyAttempted | A specific extraction strategy is being tried |
ExtractionStrategySucceeded | The strategy produced a result |
ExtractionStrategyFailed | The strategy did not produce a result |
Streaming Events (Events\PartialsGenerator)
| Event | When |
|---|---|
ChunkReceived | A raw chunk arrived from the provider |
StreamedResponseReceived | A streamed response chunk was processed |
StreamedResponseFinished | The stream has ended |
PartialJsonReceived | A partial JSON fragment was accumulated |
PartialResponseGenerated | A partial deserialized response is available |
PartialResponseGenerationFailed | Partial deserialization failed (non-fatal) |
StreamedToolCallStarted | A tool call began in the stream |
StreamedToolCallUpdated | A tool call received more data |
StreamedToolCallCompleted | A tool call finished |
Event Methods
Every event inherits the following convenience methods fromCognesy\Events\Event:
| Method | Description |
|---|---|
print() | Print a console-friendly representation |
printLog() | Print a log-formatted representation |
printDebug() | Print console output and dump the full event object |
asConsole() | Return the event formatted for console output |
asLog() | Return the event formatted for log output |
toArray() | Return the event data as an associative array |
name() | Return the short class name of the event |
$logLevel property (PSR log level) and a $data payload. The
print() method respects a configurable log-level threshold.
Instructor event payloads are normalized arrays. Structured-output lifecycle events
also expose correlation fields such as requestId, executionId, attemptId,
phase, and phaseId where applicable.