Skip to content

Doc: Update README examples to prevent double-wrapping of embedding functions#2432

Merged
danielaskdd merged 3 commits intoHKUDS:mainfrom
danielaskdd:embedding-example
Nov 28, 2025
Merged

Doc: Update README examples to prevent double-wrapping of embedding functions#2432
danielaskdd merged 3 commits intoHKUDS:mainfrom
danielaskdd:embedding-example

Conversation

@danielaskdd
Copy link
Collaborator

Fix: Update README examples to prevent double-wrapping of embedding functions

Problem:
The EmbeddingFunc.__call__ method now contains processing logic that prevents functions from being wrapped twice. This causes issues when embedding functions already decorated with @wrap_embedding_func_with_attrs are wrapped again using EmbeddingFunc().

Root Cause:
Since openai_embed, ollama_embed, and similar functions are already decorated with @wrap_embedding_func_with_attrs, wrapping them again with EmbeddingFunc() results in double decoration, which breaks the expected behavior.

Solution:
Update all README examples that incorrectly use EmbeddingFunc() to wrap pre-decorated embedding functions. The correct approach is to either:

  1. Use @wrap_embedding_func_with_attrs decorator to create a custom embedding function that calls xxx_embed.func (the unwrapped function)
  2. Pass the decorated embedding function directly to LightRAG without additional wrapping

Before (Incorrect - Double Wrapping):

embedding_func=EmbeddingFunc(
    embedding_dim=768,
    func=lambda texts: ollama_embed(texts, ...)  # ollama_embed is already decorated!
)

After (Correct):

@wrap_embedding_func_with_attrs(embedding_dim=768, max_token_size=8192)
async def embedding_func(texts: list[str]) -> np.ndarray:
    return await ollama_embed.func(texts, ...)  # Call .func to access the unwrapped function

rag = LightRAG(
    ...
    embedding_func=embedding_func,  # Pass directly, no EmbeddingFunc wrapper needed
)

Key Points:

  • Call xxx_embed.func instead of xxx_embed to avoid triggering double decoration
  • The .func attribute accesses the underlying function while preserving the built-in retry logic
  • Pass decorated functions directly to embedding_func parameter without additional EmbeddingFunc() wrapper

@danielaskdd
Copy link
Collaborator Author

@codex review

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Can't wait for the next one!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielaskdd danielaskdd merged commit 1b02684 into HKUDS:main Nov 28, 2025
4 checks passed
@danielaskdd danielaskdd deleted the embedding-example branch November 28, 2025 07:32
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.

1 participant