feat(llm): extract BaseAnthropicLLM, add base_url, export extension points#566
Open
matteomedioli wants to merge 8 commits into
Open
feat(llm): extract BaseAnthropicLLM, add base_url, export extension points#566matteomedioli wants to merge 8 commits into
matteomedioli wants to merge 8 commits into
Conversation
…d schema logic Moves the message-building, schema-conversion, and response-parsing logic that lived directly on AnthropicLLM into a new BaseAnthropicLLM base class, mirroring the BaseOpenAILLM/OpenAILLM split. AnthropicLLM becomes a subclass responsible only for constructing the SDK clients, paving the way for alternate Anthropic-compatible client implementations.
Allows routing Anthropic requests to a custom Anthropic-compatible endpoint by passing base_url through to both the sync and async SDK clients, alongside the existing http_client routing logic.
Add both base classes to neo4j_graphrag.llm's imports and __all__ so they are documented, supported entry points for subclassing custom LLM clients, with a test verifying the exports.
Covers the AnthropicLLM/BaseAnthropicLLM split: confirms AnthropicLLM subclasses BaseAnthropicLLM, exercises invoke/schema logic through a minimal BaseAnthropicLLM subclass, and verifies the new base_url parameter reaches both the sync and async Anthropic SDK clients (alone and combined with an explicit http_client).
…sses Adds a Sphinx docs page explaining what BaseAnthropicLLM and BaseOpenAILLM are for and how the http_client/base_url injection contract works, with a worked example of subclassing BaseAnthropicLLM to reach a custom endpoint. Links the page and adds API reference entries for both base classes.
Adds a changelog entry covering the new BaseAnthropicLLM base class, the base_url parameter on AnthropicLLM, the BaseAnthropicLLM/BaseOpenAILLM exports, and the new LLM extensibility docs page.
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 (the
http_clientsync/async fix), this PR extends the same "shared base class, thin provider subclass" shape already used for OpenAI (BaseOpenAILLM→OpenAILLM/AzureOpenAILLM) to Anthropic.AnthropicLLMtoday mixes two concerns in one class: provider-shape logic (message building, JSON-schema conversion, response parsing) and SDK client construction. There's no supported way to reuse the provider-shape logic against a different Anthropic-compatible endpoint without copying the whole class.Changes:
BaseAnthropicLLM(LLMBase, abc.ABC)holding all ofAnthropicLLM's message-building, schema-conversion, and response-parsing logic, moved verbatim — no behavior change.AnthropicLLMbecomes a thin subclass ofBaseAnthropicLLMwhose__init__only constructsanthropic.Anthropic/anthropic.AsyncAnthropic(reusing the sync/asynchttp_clientsplit from fix(llm): route http_client to the matching sync/async Anthropic client #565).base_url: str | None = Nonekeyword parameter toAnthropicLLM, passed through to both SDK clients — previously only reachable as an undocumented**kwargspassthrough accident.BaseAnthropicLLMandBaseOpenAILLMfromneo4j_graphrag.llmand add them to__all__—BaseOpenAILLMalready existed but was never exported, so this also fixes that gap for OpenAI as a side effect of applying one consistent policy to both families.docs/source/llm_extensibility.rst, linked from the docs index and API reference) documenting thehttp_client/base_urlcontract shared by both base classes, with a worked subclassing example.Every provider SDK already supports pointing at a custom endpoint over a custom transport — the limitation was never at the SDK level, only in how these classes exposed (or didn't expose) that as a documented constructor contract. With this change, anyone running a model behind an Anthropic-compatible endpoint — a self-hosted gateway, an internal proxy, a compatibility layer, or simply a different region/deployment — can write a small subclass instead of forking
AnthropicLLMor relying on**kwargsaccidents:All message-building, schema conversion, and response parsing is inherited — nothing provider-shape-related needs reimplementing.
Only 2 of our 8 LLM providers follow this shape today —
AnthropicLLM/BaseAnthropicLLM(this PR) andOpenAILLM/AzureOpenAILLM/BaseOpenAILLM(pre-existing). The other six —CohereLLM,MistralAILLM,OllamaLLM,BedrockLLM,GeminiLLM, andVertexAILLM— still mix provider-shape logic and SDK client construction in one class, with noBase<X>LLMsplit and no exported extension point. Worth a follow-up pass to align each of them where the provider's SDK actually has a client object to factor out; a couple, like Bedrock (boto3 session/endpoint_url, not an httpx client) and VertexAI (no per-instance client at all, driven by globalvertexai.init()), will need a different-shaped extension point. Not proposing that fix here, just flagging the inconsistency as worth scoping separately.Type of Change
Complexity
Complexity: Medium
How Has This Been Tested?
BaseAnthropicLLMholds all previously-AnthropicLLMlogic, unchanged in behavior;base_urlverified passed through to bothanthropic.Anthropicandanthropic.AsyncAnthropic;BaseAnthropicLLM/BaseOpenAILLMverified importable fromneo4j_graphrag.llmand present in__all__; fulltests/unit/llm/suite green;mypy/ruffclean; new docs page builds without new Sphinx warnings.Checklist
The following requirements should have been met (depending on the changes in the branch):