refactor(llm): extract shared http_client sync/async split helper#567
Open
matteomedioli wants to merge 1 commit into
Open
refactor(llm): extract shared http_client sync/async split helper#567matteomedioli wants to merge 1 commit into
matteomedioli wants to merge 1 commit into
Conversation
AnthropicLLM, OpenAILLM, and AzureOpenAILLM each carried an identical ~10-line dance to route an httpx.Client/httpx.AsyncClient http_client kwarg to the matching SDK client, warning and dropping it otherwise. Factored into one split_http_client_kwargs helper in llm/utils.py, used by all three, so any future subclass needing a custom endpoint doesn't need a fourth/fifth copy of the same logic. Pure refactor, no behavior change.
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.
Description
Stacked on top of #565. While working on that fix, we noticed the
http_clientsync/async type-routing logic — pophttp_clientfrom kwargs, route anhttpx.Clientto the sync SDK client, anhttpx.AsyncClientto the async one, warn and drop anything else — is copy-pasted verbatim three times:AnthropicLLM.__init__,OpenAILLM.__init__, andAzureOpenAILLM.__init__.Since
BaseAnthropicLLM/BaseOpenAILLMdon't do any SDK client construction themselves (by design — that's left entirely to subclasses), any future subclass built to reach a custom endpoint would otherwise become a 4th/5th copy of this exact logic. Extracting it into one shared helper now means new subclasses inherit the fix instead of copying it.Changes:
neo4j_graphrag.llm.utils.split_http_client_kwargs(kwargs) -> (sync_kwargs, async_kwargs), a single implementation of the pop/route/warn logic.AnthropicLLM.__init__,OpenAILLM.__init__, andAzureOpenAILLM.__init__now all call this helper instead of repeating theisinstancechecks and warning inline.httpx/warningsimports fromanthropic_llm.py/openai_llm.pywhere they were only present for this logic.split_http_client_kwargsintests/unit/llm/test_llm_utils.py(no-client passthrough, sync routing, async routing, invalid-type warn-and-drop, and non-mutation of the caller's input dict) — previously this logic only had indirect coverage through each provider class's own constructor tests.Pure refactor: no behavior change for any existing caller.
Type of Change
(Refactor, no behavior change — none of the boxes above are an exact fit; flagging explicitly since the template has no "refactor" option.)
Complexity
Complexity: Low
How Has This Been Tested?
New
tests/unit/llm/test_llm_utils.pycases directly exercisesplit_http_client_kwargs: nohttp_clientpassed,httpx.Clientrouted to sync only,httpx.AsyncClientrouted to async only, invalid type warns and is dropped from both, input dict is not mutated. Full existingtests/unit/llm/suite passes unchanged.mypy/ruff check/ruff format --checkall clean.Checklist
The following requirements should have been met (depending on the changes in the branch):