feat: add per-instance prompt overrides via LightRAG.prompts field#2951
feat: add per-instance prompt overrides via LightRAG.prompts field#2951
Conversation
a581a3a to
d050ea4
Compare
Add `prompts: dict[str, Any]` field to LightRAG dataclass and
`_get_prompt(key, global_config)` helper in operate.py.
All PROMPTS["..."] lookups in extraction and query paths now go through
`_get_prompt()`, which checks `global_config["prompts"]` first and falls
back to the global PROMPTS dict. Callers can inject per-instance prompt
overrides via `LightRAG(prompts={...})` without mutating the global
`lightrag.prompt.PROMPTS`, eliminating cross-contamination between
multiple LightRAG instances sharing the same process.
Backward compatible: instances without a `prompts` override behave identically to before.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cce2eba90d
ℹ️ 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".
| prompt_profile = resolve_entity_extraction_prompt_profile( | ||
| addon_params, use_json_extraction | ||
| addon_params, | ||
| use_json_extraction, | ||
| global_config, | ||
| ) |
There was a problem hiding this comment.
Apply prompt overrides to cached extraction profile
extract_entities now resolves prompt profiles with global_config only in the prompt_profile is None fallback path, but the normal LightRAG path uses a precomputed _entity_extraction_prompt_profile cache that is still built without per-instance prompts. As a result, overrides like default_entity_types_guidance and entity_extraction_examples are silently ignored for typical instances, so the new per-instance prompt feature does not work for entity extraction defaults unless callers bypass the cached path.
Useful? React with 👍 / 👎.
| def _get_prompt(key: str, global_config: dict | None = None) -> Any: | ||
| """Return per-instance prompt override if set, else fall back to global PROMPTS.""" | ||
| if global_config: | ||
| override = global_config.get("prompts", {}).get(key) |
There was a problem hiding this comment.
Handle null
prompts before override lookup
_get_prompt assumes global_config["prompts"] is a mapping and immediately calls .get(key) on it; if a caller passes prompts=None (common when config values come from YAML/JSON nulls), this raises AttributeError and breaks prompt rendering across query/extraction flows. The helper should validate/coerce prompts to a dict before nested lookup.
Useful? React with 👍 / 👎.
Description
Add per-instance prompt override support to LightRAG. Callers can now pass
prompts={...}to theLightRAGconstructor to override specific prompt templates without mutating the globallightrag.prompt.PROMPTSdict.This eliminates cross-contamination between multiple
LightRAGinstances running in the same process with different prompt configurations.Related Issues
See also #1353
Changes Made
lightrag/lightrag.py: Addprompts: dict[str, Any]field to theLightRAGdataclass (default_factory=dict). Included inglobal_configautomatically viaasdict(self).lightrag/operate.py: Add_get_prompt(key, global_config=None)helper. Checksglobal_config["prompts"]first; falls back to globalPROMPTSif no override is set. Replace all directPROMPTS["..."]accesses in extraction and query paths with_get_prompt("...", global_config).Checklist
Additional Notes
Backward compatible. Instances that do not pass
prompts=behave identically to before —_get_prompt()falls back to the globalPROMPTSdict unconditionally when the per-instance override dict is empty.Two delimiter constants (
DEFAULT_TUPLE_DELIMITER,DEFAULT_COMPLETION_DELIMITER) in_rebuild_from_extraction_result()still referencePROMPTSdirectly, as that function has no access toglobal_config. Overriding these two keys viaprompts=is therefore not supported and would cause a parsing mismatch against cached extraction results. All other prompt keys are fully overridable.Note: prepared with claude code