Skip to content

Automatic Function Calling doesn't support parametric generics within union type function argument #22

@pablofgaeta

Description

@pablofgaeta

I found that the library is unable to parse a function tool parameter if it's a union type and contains a parametrized generic. The example type in my case was Optional[list[str]]. The error occurs when using the automatic function calling functionality.

When iterating over the union types, isinstance(value, arg) is called, I believe the fix is to modify this to isinstance(value, get_origin(arg)) on this line:

if isinstance(value, arg) or (

The function tool worked correctly after making this modification.

Environment details

  • Programming language: Python
  • OS: macOS
  • Language runtime version: 3.12.7
  • Package version: google-genai==0.2.2

Steps to reproduce

from typing import Optional

from google import genai
from google.genai import types as genai_types

def find_products(
    max_results: int = 3,
    brands: Optional[list[str]] = None,
):
    """Search for top popular products. Optionally filter for certain brands.

    Args:
        max_results (int): The max number of results to be returned.
        brands (Optional[list[str]]): List of brands to filter products.
    """

    return "Not Implemented."

genai_client = genai.Client(vertexai=True, project=PROJECT_ID, location=REGION)

generate_content_response = genai_client.models.generate_content(
    model="gemini-1.5-flash-002",
    contents="What gucci products do you sell?",
    config=genai_types.GenerateContentConfig(
        system_instruction="You are a Cymbal Retail chat assistant. Help answer any user questions.",
        temperature=0.2,
        candidate_count=1,
        seed=42,
        tools=[find_products],
    ),
)

print(generate_content_response)

When I run this snippet, the library catches the error 'isinstance() argument 2 cannot be a parameterized generic' and the full output is:

candidates=[Candidate(content=Content(parts=[Part(video_metadata=None, code_execution_result=None, executable_code=None, file_data=None, function_call=None, function_response=None, inline_data=None, text='I am sorry, I cannot fulfill this request. There was an error when I tried to search for Gucci products.')], role='model'), citation_metadata=None, finish_message=None, token_count=None, avg_logprobs=-0.07933367853579314, finish_reason='STOP', grounding_metadata=None, index=None, logprobs_result=None, safety_ratings=None)] model_version='gemini-1.5-flash-002' prompt_feedback=None usage_metadata=GenerateContentResponseUsageMetadata(cached_content_token_count=None, candidates_token_count=23, prompt_token_count=105, total_token_count=128) automatic_function_calling_history=[Content(parts=[Part(video_metadata=None, code_execution_result=None, executable_code=None, file_data=None, function_call=None, function_response=None, inline_data=None, text='What gucci products do you sell?')], role='user'), Content(parts=[Part(video_metadata=None, code_execution_result=None, executable_code=None, file_data=None, function_call=FunctionCall(id=None, args={'max_results': 10, 'brands': ['gucci']}, name='find_products'), function_response=None, inline_data=None, text=None)], role='model'), Content(parts=[Part(video_metadata=None, code_execution_result=None, executable_code=None, file_data=None, function_call=None, function_response=FunctionResponse(id=None, name='find_products', response={'error': 'isinstance() argument 2 cannot be a parameterized generic'}), inline_data=None, text=None)], role='user')] parsed=None

Metadata

Metadata

Assignees

Labels

priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions