Skip to content

feat: Support instance methods for automatic function calling in Google GenAI Java SDK. - #159

Open
copybara-service[bot] wants to merge 1 commit into
mainfrom
copybara_964733917
Open

feat: Support instance methods for automatic function calling in Google GenAI Java SDK.#159
copybara-service[bot] wants to merge 1 commit into
mainfrom
copybara_964733917

Conversation

@copybara-service

Copy link
Copy Markdown
Contributor

feat: Support instance methods for automatic function calling in Google GenAI Java SDK.

Automatic Function Calling (AFC) previously restricted function declarations and invocations to public static methods only, throwing an IllegalArgumentException ("Instance methods are not supported. Please use static methods.") if an instance method was provided. This forced users to encapsulate state in static fields or create static wrappers when calling methods on service objects.

Support passing an instance or instance mapping along with Method references when configuring tools for Automatic Function Calling:

  • Remove static modifier validation in FunctionDeclaration.fromMethod(...) so schemas can be generated from instance methods.
  • Add function_instances mapping field to Tool definition and builder overloads to Tool.Builder:
    • functions(Method, Object)
    • functions(List<Method>, Object)
    • functions(Map<Method, Object>)
  • Update AfcUtil to extract function instances from GenerateContentConfig and invoke target methods with their bound instance (method.invoke(instance, args)).
  • Pass functionInstanceMap across synchronous and asynchronous model generation loops (Models, AsyncModels, Chat, and AsyncChat).
  • Add unit tests in FunctionDeclarationTest, AfcUtilTest, and PrivateChatAfcTest.

Closes googleapis/java-genai#807

…le GenAI Java SDK.

Automatic Function Calling (AFC) previously restricted function declarations and invocations to public static methods only, throwing an `IllegalArgumentException` ("Instance methods are not supported. Please use static methods.") if an instance method was provided. This forced users to encapsulate state in static fields or create static wrappers when calling methods on service objects.

Support passing an instance or instance mapping along with `Method` references when configuring tools for Automatic Function Calling:
- Remove static modifier validation in `FunctionDeclaration.fromMethod(...)` so schemas can be generated from instance methods.
- Add `function_instances` mapping field to `Tool` definition and builder overloads to `Tool.Builder`:
  - `functions(Method, Object)`
  - `functions(List<Method>, Object)`
  - `functions(Map<Method, Object>)`
- Update `AfcUtil` to extract function instances from `GenerateContentConfig` and invoke target methods with their bound instance (`method.invoke(instance, args)`).
- Pass `functionInstanceMap` across synchronous and asynchronous model generation loops (`Models`, `AsyncModels`, `Chat`, and `AsyncChat`).
- Add unit tests in `FunctionDeclarationTest`, `AfcUtilTest`, and `PrivateChatAfcTest`.

Closes googleapis/java-genai#807

PiperOrigin-RevId: 964733917
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FR: Support Automatic Function Calling (AFC) with non-static instance methods

1 participant