From 527413db53426d75ce8d35eae2f18423d1521e2e Mon Sep 17 00:00:00 2001 From: isatyamks Date: Sun, 15 Dec 2024 23:26:16 +0530 Subject: [PATCH 1/5] refactor: improve docstring parsing logic for Google style compliance --- .../langchain_core/utils/function_calling.py | 57 +++++++++---------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/libs/core/langchain_core/utils/function_calling.py b/libs/core/langchain_core/utils/function_calling.py index 4779d26244203..39106712ac8b2 100644 --- a/libs/core/langchain_core/utils/function_calling.py +++ b/libs/core/langchain_core/utils/function_calling.py @@ -608,38 +608,32 @@ def _parse_google_docstring( Assumes the function docstring follows Google Python style guide. """ - if docstring: - docstring_blocks = docstring.split("\n\n") + if not docstring: if error_on_invalid_docstring: - filtered_annotations = { - arg for arg in args if arg not in ("run_manager", "callbacks", "return") - } - if filtered_annotations and ( - len(docstring_blocks) < 2 or not docstring_blocks[1].startswith("Args:") - ): - msg = "Found invalid Google-Style docstring." - raise ValueError(msg) - descriptors = [] - args_block = None - past_descriptors = False - for block in docstring_blocks: - if block.startswith("Args:"): - args_block = block - break - elif block.startswith(("Returns:", "Example:")): - # Don't break in case Args come after - past_descriptors = True - elif not past_descriptors: - descriptors.append(block) - else: - continue - description = " ".join(descriptors) - else: - if error_on_invalid_docstring: - msg = "Found invalid Google-Style docstring." - raise ValueError(msg) - description = "" - args_block = None + raise ValueError("Found invalid Google-Style docstring.") + return "", {} + + docstring_blocks = docstring.split("\n\n") + + if error_on_invalid_docstring: + filtered_annotations = { + arg for arg in args if arg not in ("run_manager", "callbacks", "return") + } + has_args_section = any(block.startswith("Args:") for block in docstring_blocks) + if filtered_annotations and not has_args_section: + raise ValueError("Found invalid Google-Style docstring.") + description_blocks = [] + args_block = None + for block in docstring_blocks: + if block.startswith("Args:"): + args_block = block + break + elif block.startswith(("Returns:", "Example:")): + break + else: + description_blocks.append(block) + + description = " ".join(description_blocks).strip() arg_descriptions = {} if args_block: arg = None @@ -652,6 +646,7 @@ def _parse_google_docstring( return description, arg_descriptions + def _py_38_safe_origin(origin: type) -> type: origin_union_type_map: dict[type, Any] = ( {types.UnionType: Union} if hasattr(types, "UnionType") else {} From 5adc688f4cfca8962f6fbd8ea469f6dc2dd97095 Mon Sep 17 00:00:00 2001 From: isatyamks Date: Sun, 15 Dec 2024 23:39:42 +0530 Subject: [PATCH 2/5] fix: improve error messages for invalid Google-Style docstrings --- libs/core/langchain_core/utils/function_calling.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libs/core/langchain_core/utils/function_calling.py b/libs/core/langchain_core/utils/function_calling.py index 39106712ac8b2..bddcf99236782 100644 --- a/libs/core/langchain_core/utils/function_calling.py +++ b/libs/core/langchain_core/utils/function_calling.py @@ -610,7 +610,8 @@ def _parse_google_docstring( """ if not docstring: if error_on_invalid_docstring: - raise ValueError("Found invalid Google-Style docstring.") + msg = "Found invalid Google-Style docstring." + raise ValueError(msg) return "", {} docstring_blocks = docstring.split("\n\n") @@ -621,7 +622,9 @@ def _parse_google_docstring( } has_args_section = any(block.startswith("Args:") for block in docstring_blocks) if filtered_annotations and not has_args_section: - raise ValueError("Found invalid Google-Style docstring.") + msg = "Found invalid Google-Style docstring." + raise ValueError(msg) + description_blocks = [] args_block = None for block in docstring_blocks: @@ -643,10 +646,12 @@ def _parse_google_docstring( arg_descriptions[arg.strip()] = desc.strip() elif arg: arg_descriptions[arg.strip()] += " " + line.strip() + return description, arg_descriptions + def _py_38_safe_origin(origin: type) -> type: origin_union_type_map: dict[type, Any] = ( {types.UnionType: Union} if hasattr(types, "UnionType") else {} From 87ae4fce2dc0a6ed297cdc1c5ccb1a09f7aaab21 Mon Sep 17 00:00:00 2001 From: Satyam Kumar Date: Sun, 15 Dec 2024 23:43:40 +0530 Subject: [PATCH 3/5] fix:found invalid Google-Style docstring. --- .../langchain_core/utils/function_calling.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libs/core/langchain_core/utils/function_calling.py b/libs/core/langchain_core/utils/function_calling.py index db68f3e0f7abb..2badc8a306ab4 100644 --- a/libs/core/langchain_core/utils/function_calling.py +++ b/libs/core/langchain_core/utils/function_calling.py @@ -610,7 +610,8 @@ def _parse_google_docstring( """ if not docstring: if error_on_invalid_docstring: - raise ValueError("Found invalid Google-Style docstring.") + msg = "Found invalid Google-Style docstring." + raise ValueError(msg) return "", {} docstring_blocks = docstring.split("\n\n") @@ -621,7 +622,9 @@ def _parse_google_docstring( } has_args_section = any(block.startswith("Args:") for block in docstring_blocks) if filtered_annotations and not has_args_section: - raise ValueError("Found invalid Google-Style docstring.") + msg = "Found invalid Google-Style docstring." + raise ValueError(msg) + description_blocks = [] args_block = None for block in docstring_blocks: @@ -640,13 +643,10 @@ def _parse_google_docstring( for line in args_block.split("\n")[1:]: if ":" in line: arg, desc = line.split(":", maxsplit=1) - arg = arg.strip() - arg_name, _, _annotations = arg.partition(" ") - if _annotations.startswith("(") and _annotations.endswith(")"): - arg = arg_name - arg_descriptions[arg] = desc.strip() + arg_descriptions[arg.strip()] = desc.strip() elif arg: - arg_descriptions[arg] += " " + line.strip() + arg_descriptions[arg.strip()] += " " + line.strip() + return description, arg_descriptions From 94aa03f22dfdca632f5e3e4f26ef32e42a70d459 Mon Sep 17 00:00:00 2001 From: isatyamks Date: Sun, 15 Dec 2024 23:51:21 +0530 Subject: [PATCH 4/5] fix: clean up whitespace and resolve merge conflicts in Google-Style docstring parser --- .../langchain_core/utils/function_calling.py | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/libs/core/langchain_core/utils/function_calling.py b/libs/core/langchain_core/utils/function_calling.py index 664370319c310..052a7819b8df3 100644 --- a/libs/core/langchain_core/utils/function_calling.py +++ b/libs/core/langchain_core/utils/function_calling.py @@ -613,9 +613,7 @@ def _parse_google_docstring( msg = "Found invalid Google-Style docstring." raise ValueError(msg) return "", {} - docstring_blocks = docstring.split("\n\n") - if error_on_invalid_docstring: filtered_annotations = { arg for arg in args if arg not in ("run_manager", "callbacks", "return") @@ -624,7 +622,6 @@ def _parse_google_docstring( if filtered_annotations and not has_args_section: msg = "Found invalid Google-Style docstring." raise ValueError(msg) - description_blocks = [] args_block = None for block in docstring_blocks: @@ -635,7 +632,6 @@ def _parse_google_docstring( break else: description_blocks.append(block) - description = " ".join(description_blocks).strip() arg_descriptions = {} if args_block: @@ -645,22 +641,11 @@ def _parse_google_docstring( arg, desc = line.split(":", maxsplit=1) arg_descriptions[arg.strip()] = desc.strip() elif arg: -<<<<<<< HEAD -<<<<<<< HEAD - arg_descriptions[arg.strip()] += " " + line.strip() - -======= - arg_descriptions[arg] += " " + line.strip() ->>>>>>> e77052b4a51b9a662871c0680d34e51d04d8b317 -======= arg_descriptions[arg.strip()] += " " + line.strip() - ->>>>>>> 87ae4fce2dc0a6ed297cdc1c5ccb1a09f7aaab21 return description, arg_descriptions - def _py_38_safe_origin(origin: type) -> type: origin_union_type_map: dict[type, Any] = ( {types.UnionType: Union} if hasattr(types, "UnionType") else {} @@ -698,4 +683,4 @@ def _recursive_set_additional_properties_false( if "items" in schema: _recursive_set_additional_properties_false(schema["items"]) - return schema + return schema \ No newline at end of file From c2d615d82a8d41d3ecbf4b9290179a7fccbff0b2 Mon Sep 17 00:00:00 2001 From: isatyamks Date: Sun, 15 Dec 2024 23:53:54 +0530 Subject: [PATCH 5/5] fix: remove unnecessary newline at the end of function_calling.py --- libs/core/langchain_core/utils/function_calling.py | 1 - 1 file changed, 1 deletion(-) diff --git a/libs/core/langchain_core/utils/function_calling.py b/libs/core/langchain_core/utils/function_calling.py index 052a7819b8df3..d17d0db71be09 100644 --- a/libs/core/langchain_core/utils/function_calling.py +++ b/libs/core/langchain_core/utils/function_calling.py @@ -682,5 +682,4 @@ def _recursive_set_additional_properties_false( _recursive_set_additional_properties_false(value) if "items" in schema: _recursive_set_additional_properties_false(schema["items"]) - return schema \ No newline at end of file