97e91a83f3
Ruff / Ruff (push) Has been cancelled
Test / Core Tests (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.10) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.11) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.12) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.13) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.9) (push) Has been cancelled
Test / Full Coverage (Python 3.11) (push) Has been cancelled
Test / Core Provider Tests (OpenAI) (push) Has been cancelled
Test / Core Provider Tests (Anthropic) (push) Has been cancelled
Test / Core Provider Tests (Google) (push) Has been cancelled
Test / Core Provider Tests (Other) (push) Has been cancelled
Test / Anthropic Tests (push) Has been cancelled
Test / Gemini Tests (push) Has been cancelled
Test / Google GenAI Tests (push) Has been cancelled
Test / Vertex AI Tests (push) Has been cancelled
Test / OpenAI Tests (push) Has been cancelled
Test / Writer Tests (push) Has been cancelled
Test / Auto Client Tests (push) Has been cancelled
ty / type-check (push) Has been cancelled
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.