97e91a83f3
Ruff / Ruff (push) Waiting to run
Test / Core Tests (push) Waiting to run
Test / Offline Coverage Tests (Python 3.10) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.11) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.12) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.13) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.9) (push) Waiting to run
Test / Full Coverage (Python 3.11) (push) Waiting to run
Test / Core Provider Tests (OpenAI) (push) Blocked by required conditions
Test / Core Provider Tests (Anthropic) (push) Blocked by required conditions
Test / Core Provider Tests (Google) (push) Blocked by required conditions
Test / Core Provider Tests (Other) (push) Blocked by required conditions
Test / Anthropic Tests (push) Blocked by required conditions
Test / Gemini Tests (push) Blocked by required conditions
Test / Google GenAI Tests (push) Blocked by required conditions
Test / Vertex AI Tests (push) Blocked by required conditions
Test / OpenAI Tests (push) Blocked by required conditions
Test / Writer Tests (push) Blocked by required conditions
Test / Auto Client Tests (push) Blocked by required conditions
ty / type-check (push) Waiting to run
Instructor Hooks Example
This example demonstrates how to use the Hooks system in the Instructor library to monitor, log, and debug your LLM interactions.
What are Hooks?
Hooks provide a powerful mechanism for intercepting and handling events during the completion and parsing process. They allow you to add custom behavior, logging, or error handling at various stages of the API interaction.
The Instructor library supports several predefined hooks:
completion:kwargs: Emitted when completion arguments are providedcompletion:response: Emitted when a completion response is receivedcompletion:error: Emitted when an error occurs during completioncompletion:last_attempt: Emitted when the last retry attempt is madeparse:error: Emitted when an error occurs during response parsing
What This Example Shows
This example demonstrates:
- Basic Hook Registration: How to register handlers for different hook events
- Multiple Handlers: How to register multiple handlers for the same event
- Statistics Collection: How to collect and track API usage statistics
- Error Handling: How to catch and process different types of errors
- Hook Cleanup: How to remove hooks when they're no longer needed
Usage Examples
The code demonstrates three scenarios:
- Successful Extraction: A basic example that works correctly
- Parse Error: An example that triggers a validation error
- Multiple Hooks: Shows how to attach multiple handlers to the same event
How to Run the Example
# Navigate to the hooks example directory
cd examples/hooks
# Run the example
python run.py
Expected Output
The example will print detailed information about each request, including:
- 🔍 Request details (model, prompt)
- 📏 Approximate input token count
- 📊 Token usage statistics
- ✅ Successful responses
- ⚠️ Parse errors
- ❌ Completion errors
- 🔄 Retry attempt notifications
At the end, it will print a summary of the statistics collected.
Learn More
For more information about hooks in Instructor, see the hooks documentation.