Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve docstring parsing logic for Google style compliance #28729

Closed
wants to merge 8 commits into from
63 changes: 26 additions & 37 deletions libs/core/langchain_core/utils/function_calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,54 +608,44 @@

Assumes the function docstring follows Google Python style guide.
"""
if docstring:
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")
}
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 not docstring:
if error_on_invalid_docstring:
msg = "Found invalid Google-Style docstring."
raise ValueError(msg)
description = ""
args_block = None
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:
msg = "Found invalid Google-Style docstring."
raise ValueError(msg)
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
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



def _py_38_safe_origin(origin: type) -> type:
origin_union_type_map: dict[type, Any] = (
{types.UnionType: Union} if hasattr(types, "UnionType") else {}
Expand Down Expand Up @@ -692,5 +682,4 @@
_recursive_set_additional_properties_false(value)
if "items" in schema:
_recursive_set_additional_properties_false(schema["items"])

return schema
return schema

Check failure on line 685 in libs/core/langchain_core/utils/function_calling.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.12

Ruff (W292)

langchain_core/utils/function_calling.py:685:18: W292 No newline at end of file

Check failure on line 685 in libs/core/langchain_core/utils/function_calling.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.9

Ruff (W292)

langchain_core/utils/function_calling.py:685:18: W292 No newline at end of file

Check failure on line 685 in libs/core/langchain_core/utils/function_calling.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.11

Ruff (W292)

langchain_core/utils/function_calling.py:685:18: W292 No newline at end of file

Check failure on line 685 in libs/core/langchain_core/utils/function_calling.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.10

Ruff (W292)

langchain_core/utils/function_calling.py:685:18: W292 No newline at end of file

Check failure on line 685 in libs/core/langchain_core/utils/function_calling.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.13

Ruff (W292)

langchain_core/utils/function_calling.py:685:18: W292 No newline at end of file
Loading