Skip to content

feat(llm): extract BaseAnthropicLLM, add base_url, export extension points - #566

Merged
matteomedioli merged 13 commits into
mainfrom
matteo/base-anthropic-llm
Jul 23, 2026
Merged

feat(llm): extract BaseAnthropicLLM, add base_url, export extension points#566
matteomedioli merged 13 commits into
mainfrom
matteo/base-anthropic-llm

Conversation

@matteomedioli

@matteomedioli matteomedioli commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Stack

Merge order: #565#567#566#571 / #572 (last two in either order). Each PR builds on the previous one. Until its base merges, GitHub may show parent commits in the diff — review only the commits unique to this branch.

Description

Today, using AnthropicLLM's logic against a different endpoint — a proxy, an internal gateway, a self-hosted compatible API — means forking the whole class. This PR makes it a small subclass instead, using the same pattern OpenAI already has (BaseOpenAILLMOpenAILLM / AzureOpenAILLM).

  • New BaseAnthropicLLM holds everything that doesn't depend on how the connection is made: message building, schema conversion, response parsing, structured output. AnthropicLLM becomes a thin subclass that only creates the two SDK clients. Logic moved as-is — no behavior change (full unit suite passes unchanged).
  • AnthropicLLM and OpenAILLM both gain an explicit base_url parameter to point at a custom endpoint. It already worked when passed through **kwargs; the explicit parameter makes it visible in the signature, docstring, and API docs, and keeps the two classes symmetric.
  • BaseAnthropicLLM, BaseOpenAILLM, and split_http_client_kwargs are now exported from neo4j_graphrag.llm as supported extension points (BaseOpenAILLM existed but was never exported).
  • New docs page (llm_extensibility.rst) explains the base_url / http_client contract, with a working subclass example.

Notes for reviewers — deliberate decisions, happy to change any of them:

Type of Change

  • New feature
  • Bug fix
  • Breaking change
  • Documentation update
  • Project configuration change

Complexity

Complexity: Medium

How Has This Been Tested?

  • Unit tests
  • E2E tests
  • Manual tests

Checklist

  • Documentation has been updated
  • Unit tests have been updated
  • Examples have been updated
  • CLA (https://neo4j.com/developer/cla/) has been signed
  • CHANGELOG.md updated

@matteomedioli
matteomedioli requested a review from a team as a code owner July 16, 2026 23:25
@matteomedioli
matteomedioli force-pushed the matteo/base-anthropic-llm branch from cc8c72e to 7f75af3 Compare July 17, 2026 13:24
@matteomedioli
matteomedioli changed the base branch from matteo/anthropic-kwargs-bugfix to matteo/split-http-client-helper July 17, 2026 13:24
@matteomedioli
matteomedioli force-pushed the matteo/split-http-client-helper branch from 0b4426b to 4baa924 Compare July 17, 2026 15:02
@matteomedioli
matteomedioli force-pushed the matteo/base-anthropic-llm branch from 7f75af3 to c8b38b9 Compare July 17, 2026 15:02
@matteomedioli
matteomedioli force-pushed the matteo/split-http-client-helper branch from 4baa924 to 2b6a1f6 Compare July 17, 2026 15:07
@matteomedioli
matteomedioli force-pushed the matteo/base-anthropic-llm branch from c8b38b9 to 6fca589 Compare July 17, 2026 15:07
@matteomedioli
matteomedioli force-pushed the matteo/split-http-client-helper branch from 2b6a1f6 to 5a6541c Compare July 17, 2026 15:21
@matteomedioli
matteomedioli force-pushed the matteo/base-anthropic-llm branch 2 times, most recently from cfc8dcb to 8470708 Compare July 17, 2026 15:41
Comment thread docs/source/llm_extensibility.rst Outdated
Comment thread src/neo4j_graphrag/llm/anthropic_llm.py Outdated
Comment thread src/neo4j_graphrag/llm/anthropic_llm.py
Comment thread src/neo4j_graphrag/llm/anthropic_llm.py
@matteomedioli
matteomedioli force-pushed the matteo/split-http-client-helper branch from 5a6541c to a819760 Compare July 22, 2026 11:42
@matteomedioli
matteomedioli force-pushed the matteo/base-anthropic-llm branch from 8470708 to 132e5a5 Compare July 22, 2026 13:40
Base automatically changed from matteo/split-http-client-helper to main July 22, 2026 13:47
…tests

Add unit tests verifying httpx.Client reaches only the sync Anthropic
client, httpx.AsyncClient reaches only the async client, and an invalid
http_client type warns and falls back to defaults for both clients.
Also note the stale-venv anthropic version gotcha in AGENTS.md.
…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.
…tp_client_kwargs

Review follow-ups:
- llm_extensibility.rst no longer claims OpenAILLM declares base_url as a
  constructor parameter (it accepts it via **kwargs); warning wording fixed
  (warnings.warn, not logging).
- Subclassing example now routes kwargs through split_http_client_kwargs so
  custom subclasses preserve the sync/async routing contract instead of
  reintroducing the type-mismatch bug.
- split_http_client_kwargs exported from neo4j_graphrag.llm alongside the
  base classes, with an export test.
Same explicit parameter AnthropicLLM gains in this PR, so the two
classes expose the custom-endpoint contract symmetrically. Forwarded
to both the sync and async SDK clients; behavior via **kwargs is
unchanged for existing callers. Docs and changelog updated to describe
one shared contract instead of two paths.
@matteomedioli
matteomedioli force-pushed the matteo/base-anthropic-llm branch from 132e5a5 to 7615841 Compare July 22, 2026 15:47
@matteomedioli
matteomedioli requested a review from stellasia July 22, 2026 15:58

@stellasia stellasia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the answers!

@matteomedioli
matteomedioli merged commit b98d4a9 into main Jul 23, 2026
16 checks passed
@matteomedioli
matteomedioli deleted the matteo/base-anthropic-llm branch July 23, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants