From c8c59c38b0350922a51587d3afa7ae57fad3097e Mon Sep 17 00:00:00 2001 From: yangdx Date: Mon, 1 Sep 2025 00:14:57 +0800 Subject: [PATCH] Fix entity types configuration to support JSON list parsing - Add JSON parsing for list env vars - Update entity types example format - Add list type support to get_env_value --- env.example | 2 +- lightrag/api/config.py | 2 +- lightrag/utils.py | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/env.example b/env.example index 02b3ec00..4ab64aba 100644 --- a/env.example +++ b/env.example @@ -125,7 +125,7 @@ ENABLE_LLM_CACHE_FOR_EXTRACT=true SUMMARY_LANGUAGE=English ### Entity types that the LLM will attempt to recognize -# ENTITY_TYPES=["organization", "person", "equiment", "product", "technology", "location", "event", "category"] +# ENTITY_TYPES='["Organization", "Person", "Equiment", "Product", "Technology", "Location", "Event", "Category"]' ### Chunk size for document splitting, 500~1500 is recommended # CHUNK_SIZE=1200 diff --git a/lightrag/api/config.py b/lightrag/api/config.py index eae2f45b..f17d50f0 100644 --- a/lightrag/api/config.py +++ b/lightrag/api/config.py @@ -352,7 +352,7 @@ def parse_args() -> argparse.Namespace: # Add environment variables that were previously read directly args.cors_origins = get_env_value("CORS_ORIGINS", "*") args.summary_language = get_env_value("SUMMARY_LANGUAGE", DEFAULT_SUMMARY_LANGUAGE) - args.entity_types = get_env_value("ENTITY_TYPES", DEFAULT_ENTITY_TYPES) + args.entity_types = get_env_value("ENTITY_TYPES", DEFAULT_ENTITY_TYPES, list) args.whitelist_paths = get_env_value("WHITELIST_PATHS", "/health,/api/*") # For JWT Auth diff --git a/lightrag/utils.py b/lightrag/utils.py index ab4787c4..7c92d99c 100644 --- a/lightrag/utils.py +++ b/lightrag/utils.py @@ -82,6 +82,27 @@ def get_env_value( if value_type is bool: return value.lower() in ("true", "1", "yes", "t", "on") + + # Handle list type with JSON parsing + if value_type is list: + try: + import json + + parsed_value = json.loads(value) + # Ensure the parsed value is actually a list + if isinstance(parsed_value, list): + return parsed_value + else: + logger.warning( + f"Environment variable {env_key} is not a valid JSON list, using default" + ) + return default + except (json.JSONDecodeError, ValueError) as e: + logger.warning( + f"Failed to parse {env_key} as JSON list: {e}, using default" + ) + return default + try: return value_type(value) except (ValueError, TypeError):