feat(hooks): add hook combination and per-call hooks support#1803
Conversation
- Add __add__, __iadd__, combine(), and copy() methods to Hooks class for combining hooks - Add hooks parameter to all create methods (create, create_partial, create_iterable, create_with_completion) - Per-call hooks are combined with client hooks using the + operator - Client hooks remain unchanged and functional after per-call hook usage - Add comprehensive examples demonstrating hook combination and per-call usage - Maintain backward compatibility - existing code continues to work unchanged This enables flexible hook composition and per-request customization while preserving the existing client-level hook functionality.
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
instructor | 2e0ed6d | Sep 04 2025, 08:06 PM |
|
Claude finished @jxnl's task —— View job Code Review: Hook Combination and Per-Call Hooks SupportI've completed a thorough review of this PR that adds hook combination and per-call hooks support. Here's my comprehensive analysis: Todo List
✅ Code Quality & ImplementationThe implementation demonstrates solid engineering practices: Hook Combination Logic (
Client Integration (
|
…oks. These examples are no longer needed following the recent updates to the hooks functionality, which have been documented in the main examples. This cleanup helps maintain a tidy codebase and reduces confusion for users.
|
Claude finished @jxnl's task —— View job Comprehensive PR ReviewI've completed my review of this PR that adds hook combination and per-call hooks support. Here's my comprehensive analysis: Todo List
Review SummaryThis PR adds solid hook combination and per-call hooks functionality. The implementation is generally well-designed and maintains backward compatibility. Here are my findings: ✅ Strengths
|
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed 2e0ed6d in 45 seconds. Click for details.
- Reviewed
368lines of code in2files - Skipped
0files when reviewing. - Skipped posting
2draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. examples/hooks/combination_example.py:1
- Draft comment:
The entire combination example file has been removed. Ensure that equivalent documentation or examples are provided elsewhere (e.g., in the docs or tests) and update mkdocs.yml if these examples are relocated. - Reason this comment was not posted:
Confidence changes required:80%<= threshold85%None
2. examples/hooks/per_call_hooks_example.py:1
- Draft comment:
The per-call hooks example file has been removed entirely. Please confirm that similar examples are available in the updated documentation or tests to illustrate per-call hook usage, and update mkdocs.yml if necessary. - Reason this comment was not posted:
Confidence changes required:80%<= threshold85%None
Workflow ID: wflow_3cV87ZX1JhFRRJo0
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
There was a problem hiding this comment.
Caution
Changes requested ❌
Reviewed everything up to 7ce209d in 1 minute and 57 seconds. Click for details.
- Reviewed
851lines of code in4files - Skipped
0files when reviewing. - Skipped posting
1draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. instructor/core/client.py:394
- Draft comment:
There's a placeholder comment "{{ edit_1 }}" in the parameter line. If this is not intended for production, please remove or update it. - Reason this comment was not posted:
Comment was on unchanged code.
Workflow ID: wflow_johoPM9XMPT124u7
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
| ) -> T | Any | Awaitable[T] | Awaitable[Any]: | ||
| kwargs = self.handle_kwargs(kwargs) | ||
|
|
||
| # Combine client hooks with per-call hooks |
There was a problem hiding this comment.
The hook combination logic (i.e. 'combined_hooks = self.hooks; if hooks is not None: combined_hooks = self.hooks + hooks') is repeated in multiple methods. Consider extracting this pattern into a helper method to reduce duplication and improve maintainability.
Description
This PR adds powerful hook combination capabilities and per-call hook support to the Instructor library, enabling flexible event handling and monitoring at both the client and individual request levels.
Changes
Hook Combination Support
Per-Call Hooks Support
Added parameter to all create methods:
Examples and Documentation
Key Features
Usage Examples
Hook Combination
Per-Call Hooks
Testing
This PR was written by Cursor
Important
Add hook combination and per-call hooks support to Instructor library, enabling flexible event handling and maintaining backward compatibility.
__add__,__iadd__, andcombine()methods inHooksclass for combining hook instances.copy()method added to create deep copies ofHooksinstances.hooksparameter tocreate,create_partial,create_iterable, andcreate_with_completionmethods inclient.py.+operator.This description was created by
for 2e0ed6d. You can customize this summary. It will automatically update as commits are pushed.