Core: google docstring parsing fix (#28404)

Thank you for contributing to LangChain!

- [ ] **PR title**: "core: google docstring parsing fix"


- [x] **PR message**:
- **Description:** Added a solution for invalid parsing of google
docstring such as:
    Args:
net_annual_income (float): The user's net annual income (in current year
dollars).
- **Issue:** Previous code would return arg = "net_annual_income
(float)" which would cause exception in
_validate_docstring_args_against_annotations
    - **Dependencies:** None

If no one reviews your PR within a few days, please @-mention one of
baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17.

Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
Filip Ratajczak 2024-12-09 16:27:25 -08:00 committed by GitHub
parent b78b2f7a28
commit 4e743b5427
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 2 deletions

View File

@ -646,9 +646,13 @@ def _parse_google_docstring(
for line in args_block.split("\n")[1:]: for line in args_block.split("\n")[1:]:
if ":" in line: if ":" in line:
arg, desc = line.split(":", maxsplit=1) arg, desc = line.split(":", maxsplit=1)
arg_descriptions[arg.strip()] = desc.strip() arg = arg.strip()
arg_name, _, _annotations = arg.partition(" ")
if _annotations.startswith("(") and _annotations.endswith(")"):
arg = arg_name
arg_descriptions[arg] = desc.strip()
elif arg: elif arg:
arg_descriptions[arg.strip()] += " " + line.strip() arg_descriptions[arg] += " " + line.strip()
return description, arg_descriptions return description, arg_descriptions

View File

@ -71,6 +71,19 @@ def function() -> Callable:
return dummy_function return dummy_function
@pytest.fixture()
def function_docstring_annotations() -> Callable:
def dummy_function(arg1: int, arg2: Literal["bar", "baz"]) -> None:
"""dummy function
Args:
arg1 (int): foo
arg2: one of 'bar', 'baz'
"""
return dummy_function
@pytest.fixture() @pytest.fixture()
def runnable() -> Runnable: def runnable() -> Runnable:
class Args(ExtensionsTypedDict): class Args(ExtensionsTypedDict):
@ -278,6 +291,7 @@ class DummyWithClassMethod:
def test_convert_to_openai_function( def test_convert_to_openai_function(
pydantic: type[BaseModel], pydantic: type[BaseModel],
function: Callable, function: Callable,
function_docstring_annotations: Callable,
dummy_structured_tool: StructuredTool, dummy_structured_tool: StructuredTool,
dummy_tool: BaseTool, dummy_tool: BaseTool,
json_schema: dict, json_schema: dict,
@ -311,6 +325,7 @@ def test_convert_to_openai_function(
for fn in ( for fn in (
pydantic, pydantic,
function, function,
function_docstring_annotations,
dummy_structured_tool, dummy_structured_tool,
dummy_tool, dummy_tool,
json_schema, json_schema,