Skip to main content

Events

Instructor dispatches events throughout the extraction and inference lifecycle. These events are automatically bridged to Laravel’s event system by the LaravelEventDispatcher, allowing you to listen and respond using standard Laravel patterns — listeners, subscribers, closures, and queued handlers. The bridge is implemented by Cognesy\Instructor\Laravel\Events\LaravelEventDispatcher, which lives in the packages/laravel package. It wraps Laravel’s native Illuminate\Contracts\Events\Dispatcher and forwards Instructor events to it based on your configuration.

Event Bridge Configuration

Configure event bridging in config/instructor.php:
When bridge_events is empty (the default), every Instructor event is forwarded to Laravel’s dispatcher. To reduce overhead in production, list only the event classes your listeners actually need. The bridge uses instanceof matching, so listing a parent event class also bridges its subclasses.

Available Events

All events extend Cognesy\Instructor\Events\StructuredOutputEvent (which extends Cognesy\Events\Event). Events carry data in the $data property (an array or mixed value) rather than typed properties.

Extraction Events

Namespace: Cognesy\Instructor\Events\Extraction

Response Events

Namespace: Cognesy\Instructor\Events\Response

Request Events

Namespace: Cognesy\Instructor\Events\Request

Streaming Events

Namespace: Cognesy\Instructor\Events\PartialsGenerator

Listening to Events

Using Event Listeners

Create a dedicated listener class and register it with Laravel’s event system.
Register in EventServiceProvider:

Using Closures

For lightweight listeners, register closures directly in a service provider’s boot method.

Using Event Subscribers

Group related event handlers into a single subscriber class. This is convenient when you need to handle multiple Instructor events together.
Register it in EventServiceProvider:

Common Use Cases

Logging and Monitoring

All events carry data in the $data property (typically an array). Use the name() method to get the event class short name.

Metrics and Analytics

Alerting on Failures

Queued Event Listeners

For CPU-intensive or I/O-heavy processing, implement ShouldQueue to push the work onto a queue instead of running it inline.

Wiretap (Direct Event Handling)

The wiretap method provides direct access to the raw event stream without going through Laravel’s dispatcher. This is useful for low-level debugging or when you need to observe every internal event.
The LaravelEventDispatcher itself also supports wiretap for registering global listeners that receive every event, regardless of class. These listeners run at the lowest priority after all class-specific and bridged listeners have executed.

Disabling Event Bridge

To disable event bridging entirely (for example, in high-throughput scenarios where the overhead is unacceptable):
Or via environment variable:
Disabling the bridge only stops events from being forwarded to Laravel’s dispatcher. Internal Instructor event listeners and wiretaps continue to work normally.

Testing Events

Use Laravel’s Event::fake() to assert that specific events were dispatched during a test.
Assert event data with a closure: