fix(anthropic): make Anthropic/Claude provider usable out-of-box (three bugs)#2972
Open
fix(anthropic): make Anthropic/Claude provider usable out-of-box (three bugs)#2972
Conversation
Collaborator
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Collaborator
|
The |
Fixes HKUDS#2955. The Anthropic/Claude provider had three independent defects that each blocked `anthropic_complete` under the current Anthropic SDK and LightRAG's own pipeline. 1. `voyageai` was imported unconditionally at module load. The `pipmaster` auto-install silently no-ops in a uv-managed venv, so the subsequent `import voyageai` raised ModuleNotFoundError even for users who only want the LLM side. Move the install+import inside `anthropic_embed` where it's actually needed. 2. `messages.create()` requires `max_tokens` but the wrapper never set it. Every call failed with `TypeError: Missing required arguments; Expected ('max_tokens', 'messages' and 'model') ...`. Default to 8192, overridable via **kwargs, matching the other providers. 3. `stream=True` was hardcoded, so `anthropic_complete` always returned an AsyncIterator[str]. LightRAG's KG extraction expects a plain str and crashed with `TypeError: expected string or bytes-like object, got 'async_generator'`. If the caller overrode with `stream=False` via **kwargs, the SDK returned a Message object and `async for` blew up instead. Make streaming opt-in: pop `stream` out of kwargs, pass it through explicitly, and return the concatenated text content when False; keep the existing iterator shape when True.
21717a5 to
52d4cb5
Compare
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.
Fixes #2955.
The Anthropic/Claude provider in `lightrag/llm/anthropic.py` had three independent defects that each blocked `anthropic_complete` end-to-end under the current Anthropic SDK and LightRAG's own KG extraction pipeline. All three are addressed here in one file.
1. `voyageai` imported unconditionally at module load
`pipmaster`'s auto-install silently no-ops inside a uv-managed venv (or anywhere without site-packages write permission), so the subsequent `import voyageai` at module top level raised `ModuleNotFoundError` for users who never touched Voyage embeddings at all. Moved the `install + import voyageai` pair into `anthropic_embed` so it's only required when embeddings are actually used.
2. `max_tokens` missing
`anthropic.messages.create()` requires `max_tokens`, but the wrapper never set a default and never surfaced it as a documented kwarg. Every call failed with:
```
TypeError: Missing required arguments; Expected either ('max_tokens', 'messages' and 'model')
or ('max_tokens', 'messages', 'model' and 'stream') arguments to be given
```
Added `kwargs.setdefault("max_tokens", 8192)` — matches the other providers' generous default and callers can still override via `**kwargs`.
3. `stream=True` hardcoded
The wrapper always built `create_params` with `"stream": True` and returned an `AsyncIterator[str]`, so LightRAG's KG extraction (which calls the LLM expecting a `str`) crashed with:
```
TypeError: expected string or bytes-like object, got 'async_generator'
```
A caller that overrode with `stream=False` via `**kwargs` landed on the reverse failure — the SDK returned a `Message` and the code's `async for event in response` blew up with `'async for' requires an object with aiter method, got Message`.
Made streaming opt-in: pop `stream` out of `kwargs`, thread it through explicitly, and return concatenated `content[*].text` when `stream=False` (matching `openai_complete`). The existing async-generator path stays intact for `stream=True`.
Test plan