Repository navigation
Let Python SDKs wire hand-written response hooks - #60
Conversation
|
👋 Hello @glenn-jocher, thank you for submitting a
For more guidance, please refer to our Contributing Guide. Don't hesitate to leave a comment if you have any questions. Thank you for contributing to Ultralytics! 🚀 |
UltralyticsAssistant
left a comment
There was a problem hiding this comment.
🔍 PR Review
Made with ❤️ by Ultralytics Actions
Reviewed the configuration path resolution, generated sync/async client wiring, custom-client behavior, provider copying, documentation, and end-to-end test. The opt-in hooks remain consumer-owned and default generation is unchanged. LGTM.
|
🎉 This PR has been merged—thank you, @glenn-jocher! Configurable response hooks let SDK users run custom logic after responses from generated sync and async clients, while caller-provided HTTP clients remain unchanged.
|
Summary
Adds an optional
python.hooksProviderconfig key, the response-hook counterpart ofauthProvider. It names a consumer-owned Python file that the generator copies to<package>/_hooks.py. The file must exportresponse_hook(response)andasync def async_response_hook(response). The generated sync and async clients then register them as httpxresponseevent hooks on the clients they build.http_clientis used as is (no mutation or merging), matching howtimeoutalready applies only to SDK-built clients. The README and, when the option is set, the generatedhttp_clientdocstring say so.Non-test code nets +1 line (the readme, authProvider and hooksProvider path resolution share one loop); +25 with the test.
Validation
generatePythonand the fullbun run generate) for the example config and the real ultralytics/sdk config. The latter usesauthProviderand a CLI, so both branches are covered. Generation is deterministic.Bun.serveserver on port 0 and a hooks file that prints from each hook. It calls the generated SDK sync, sync with a customhttp_client, and async, and asserts exactly["sync 200", "async 200"]. Disabling the sync wiring fails it.bun run generate,typecheck,lint,knip,test(21 pass) andbuild. With the option set, the generated package passesruff check,compilealland import.🛠️ PR Summary
Made with ❤️ by Ultralytics Actions
🌟 Summary
Adds optional Python response-hook support by copying a consumer-provided hooks module into generated SDKs and registering its hooks on SDK-created HTTP clients.
📊 Key Changes
python.hooksProvider, with relative paths resolved against the config file, and copies the module into the generated package as_hooks.py.response_hookandasync_response_hookas httpx response event hooks when the option is configured.http_clientis used unchanged and does not receive the configured hooks.🎯 Purpose & Impact