Skip to content
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions libs/genai/langchain_google_genai/_function_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,36 +283,31 @@
)


def _convert_pydantic_to_genai_function(
pydantic_model: Type[BaseModel],
tool_name: Optional[str] = None,
tool_description: Optional[str] = None,
) -> gapic.FunctionDeclaration:
if issubclass(pydantic_model, BaseModel):
schema = pydantic_model.model_json_schema()
elif issubclass(pydantic_model, BaseModelV1):
schema = pydantic_model.schema()
else:
raise NotImplementedError(
f"pydantic_model must be a Pydantic BaseModel, got {pydantic_model}"
)
schema = dereference_refs(schema)
schema.pop("definitions", None)
function_declaration = gapic.FunctionDeclaration(
name=tool_name if tool_name else schema.get("title"),
description=tool_description if tool_description else schema.get("description"),
parameters={
"properties": _get_properties_from_schema_any(
schema.get("properties")
), # TODO: use _dict_to_gapic_schema() if possible
# "items": _get_items_from_schema_any(
# schema
# ), # TODO: fix it https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/function-calling?hl#schema
"required": schema.get("required", []),
"type_": TYPE_ENUM[schema["type"]],
},
)
return function_declaration
def _convert_pydantic_to_genai_function(model: BaseModel, function_name: str = None) -> dict:

Check failure on line 286 in libs/genai/langchain_google_genai/_function_utils.py

View workflow job for this annotation

GitHub Actions / cd libs/genai / - / make lint #3.12

Ruff (E501)

langchain_google_genai/_function_utils.py:286:89: E501 Line too long (93 > 88)

Check failure on line 286 in libs/genai/langchain_google_genai/_function_utils.py

View workflow job for this annotation

GitHub Actions / cd libs/genai / - / make lint #3.9

Ruff (E501)

langchain_google_genai/_function_utils.py:286:89: E501 Line too long (93 > 88)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add test and inline citation to the Gemini function tool schema? Also run lint please 🥺

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, it will take time for me so i will try to make updates within 2 weeks.

"""
Converts a Pydantic BaseModel into a dictionary representing a Gemini function tool.
Args:
model: The Pydantic BaseModel defining the function's parameters.
function_name: Optional. The name of the function. If not provided,
the Pydantic model's class name will be used (converted to snake_case).

Check failure on line 293 in libs/genai/langchain_google_genai/_function_utils.py

View workflow job for this annotation

GitHub Actions / cd libs/genai / - / make lint #3.12

Ruff (E501)

langchain_google_genai/_function_utils.py:293:89: E501 Line too long (94 > 88)

Check failure on line 293 in libs/genai/langchain_google_genai/_function_utils.py

View workflow job for this annotation

GitHub Actions / cd libs/genai / - / make lint #3.9

Ruff (E501)

langchain_google_genai/_function_utils.py:293:89: E501 Line too long (94 > 88)
Returns:
A dictionary formatted as a Gemini function tool.
"""
if function_name is None:
# Convert CamelCase Pydantic model name to snake_case function name
function_name = ''.join(['_' + i.lower() if i.isupper() else i for i in model.__name__]).lstrip('_')

Check failure on line 300 in libs/genai/langchain_google_genai/_function_utils.py

View workflow job for this annotation

GitHub Actions / cd libs/genai / - / make lint #3.12

Ruff (E501)

langchain_google_genai/_function_utils.py:300:89: E501 Line too long (108 > 88)

Check failure on line 300 in libs/genai/langchain_google_genai/_function_utils.py

View workflow job for this annotation

GitHub Actions / cd libs/genai / - / make lint #3.9

Ruff (E501)

langchain_google_genai/_function_utils.py:300:89: E501 Line too long (108 > 88)

return {
"function_declarations": [
{
"name": function_name,
"description": model.__doc__.strip() if model.__doc__ else "",
"parameters": model.model_json_schema(),
}
]
}


def _get_properties_from_schema_any(schema: Any) -> Dict[str, Any]:
Expand Down
Loading