From b3ed98dec070f3721a8b87c00fec7c2732ef181e Mon Sep 17 00:00:00 2001 From: bachr Date: Tue, 23 Jan 2024 21:09:43 -0800 Subject: [PATCH] community[patch]: avoid KeyError when language not in LANGUAGE_SEGMENTERS (#15212) **Description:** Handle unsupported languages in same way as when none is provided **Issue:** The following line will throw a KeyError if the language is not supported. ```python self.Segmenter = LANGUAGE_SEGMENTERS[language] ``` E.g. when using `Language.CPP` we would get `KeyError: ` --------- Co-authored-by: Bagatur --- .../document_loaders/parsers/language/language_parser.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libs/community/langchain_community/document_loaders/parsers/language/language_parser.py b/libs/community/langchain_community/document_loaders/parsers/language/language_parser.py index c53dd493fde..b3f6534327f 100644 --- a/libs/community/langchain_community/document_loaders/parsers/language/language_parser.py +++ b/libs/community/langchain_community/document_loaders/parsers/language/language_parser.py @@ -97,6 +97,8 @@ class LanguageParser(BaseBlobParser): language: If None (default), it will try to infer language from source. parser_threshold: Minimum lines needed to activate parsing (0 by default). """ + if language and language not in LANGUAGE_SEGMENTERS: + raise Exception(f"No parser available for {language}") self.language = language self.parser_threshold = parser_threshold