fix: support http_client factory in OpenAIModel client_args#1893
Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Open
fix: support http_client factory in OpenAIModel client_args#1893giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Conversation
When http_client is passed as an httpx.AsyncClient instance in client_args, it gets closed after the first async with block, causing subsequent requests to fail with a closed client error. This adds support for passing http_client as a callable factory (e.g., lambda: httpx.AsyncClient(...)) that produces a fresh client on each request. Instance clients (with a .send method) are still passed through unchanged for backwards compatibility. Closes strands-agents#1036
cc81b65 to
ccf0819
Compare
Contributor
Author
|
Friendly ping — adds support for |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
http_clientis passed as anhttpx.AsyncClientinstance inclient_args, it gets closed after the firstasync withblock. Subsequent requests fail because the same closed client instance is reused.Users cannot pass a custom HTTP client to the OpenAI provider without it breaking on the second invocation.
Closes #1036
Solution
Support
http_clientas either:httpx.AsyncClientis used directlyThe distinction uses
hasattr(http_client, 'send')— real HTTP clients have a.send()method, while factory functions/lambdas do not.Usage
Changes
src/strands/models/openai.py: Updated_get_client()to resolve factory callables; updated docstringtests/strands/models/test_openai.py: 3 new tests (factory invocation, instance passthrough, no mutation)Testing
client_argsdict is not mutated