refactor: improve X402Auth streaming stability and schema filtering#147
refactor: improve X402Auth streaming stability and schema filtering#147lofingv wants to merge 6 commits intoOpenGradient:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves the X402Auth authentication handler by addressing streaming reliability issues and aligning the implementation with its documented interface. The changes ensure that request bodies can be re-read during 402 payment challenges, preventing StreamConsumed errors in streaming scenarios.
Changes:
- Added
request.read()to buffer request bodies before initial submission, enabling re-reading after 402 responses - Added missing
scheme_filterparameter to match documented API - Simplified type hints for the payment requirements selector callback
- Added validation to handle cases where no compatible payment method is found
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Add PaymentSelector type alias and pass scheme_filter to requirements selector as suggested by Copilot
|
Updated types with a Type Alias and fixed scheme_filter usage as suggested by Copilot. Ready for review |
|
Thanks! |
|
Updated the PR to match the new project structure. The x402_auth.py has been moved to the client/ folder, and I've also implemented the scheme_filter and improved type safety with PaymentSelector as we discussed. |
No problem at all! I'm really enjoying the project, so happy to jump in and help whenever I can. |
Description
This PR refactors the
X402Authhandler to ensure better reliability during streaming requests and aligns the implementation with the documented arguments.Key Changes:
request.read()before the initial request. This ensures that the request body can be re-read if a 402 challenge occurs, preventingStreamConsumederrors during LLM streaming or large POST requests.scheme_filterargument to__init__to match the existing docstring documentation and improve payment selection flexibility.selected_requirements. If no compatible payment method is found, it now logs an error and returns gracefully instead of potentially failing downstream.Testing:
httpx.Authflow correctly handles 402 responses without losing the original request context.