From 9f556573b7e28ac48128c2ff1eb21b6be7173eff Mon Sep 17 00:00:00 2001 From: Chester Curme Date: Thu, 12 Sep 2024 09:37:00 -0400 Subject: [PATCH] catch pydantic v2 schemas in dict --- .../langchain_google_vertexai/functions_utils.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/libs/vertexai/langchain_google_vertexai/functions_utils.py b/libs/vertexai/langchain_google_vertexai/functions_utils.py index f4499e8b..abdf1a56 100644 --- a/libs/vertexai/langchain_google_vertexai/functions_utils.py +++ b/libs/vertexai/langchain_google_vertexai/functions_utils.py @@ -216,10 +216,24 @@ def _format_pydantic_to_function_declaration( def _format_dict_to_function_declaration( tool: Union[FunctionDescription, Dict[str, Any]], ) -> gapic.FunctionDeclaration: + # Ensure we send "anyOf" parameters through pydantic v2 schema parsing + pydantic_version = None + if isinstance(tool, dict): + properties = tool.get("parameters", {}).get("properties", {}).values() + for property in properties: + if "anyOf" in property: + pydantic_version = "v2" + if pydantic_version: + parameters = _dict_to_gapic_schema( + tool.get("parameters", {}), pydantic_version=pydantic_version + ) + else: + parameters = _dict_to_gapic_schema(tool.get("parameters", {})) + return gapic.FunctionDeclaration( name=tool.get("name"), description=tool.get("description"), - parameters=_dict_to_gapic_schema(tool.get("parameters", {})), + parameters=parameters, )