Skip to content

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

Merged
copybara-service[bot] merged 1 commit into
mainfrom
copybara_964733917
Sep 15, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
copybara_964733917

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Aug 24, 2026

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 (Map<Method, Object>) to Tool definition.
  • Add validation in AfcUtil.getFunctionMap(...) to ensure that any non-static method has a corresponding instance in functionInstances, failing fast with a descriptive IllegalArgumentException.
  • Update AfcUtil to extract function instances from GenerateContentConfig and invoke target methods with their bound instance (method.invoke(instance, args)).
  • Safely handle optional name and args in FunctionCall, preventing NoSuchElementException and handling null map entries in AfcUtil.getFunctionResponseParts and getFunctionResponse.
  • Pass functionInstanceMap across synchronous and asynchronous model generation loops (Models, AsyncModels, Chat, and AsyncChat).
  • Add unit tests in FunctionDeclarationTest, AfcUtilTest, and PrivateChatAfcTest.

Closes #807

@copybara-service
copybara-service Bot force-pushed the copybara_964733917 branch 2 times, most recently from 2965fb4 to 6af6b61 Compare August 24, 2026 16:37
@copybara-service copybara-service Bot changed the title No public description Support instance methods for automatic function calling in Google GenAI Java SDK. Aug 24, 2026
@copybara-service
copybara-service Bot force-pushed the copybara_964733917 branch 6 times, most recently from 36ed0c9 to b4848bf Compare August 25, 2026 19:52
@copybara-service copybara-service Bot changed the title Support instance methods for automatic function calling in Google GenAI Java SDK. feat: Support instance methods for automatic function calling in Google GenAI Java SDK. Aug 25, 2026
@copybara-service
copybara-service Bot force-pushed the copybara_964733917 branch 19 times, most recently from b18dd6a to 4913311 Compare August 31, 2026 16:53
@copybara-service
copybara-service Bot force-pushed the copybara_964733917 branch 14 times, most recently from 4d284c8 to 7442369 Compare September 4, 2026 21:43
@copybara-service
copybara-service Bot force-pushed the copybara_964733917 branch 5 times, most recently from 4309c7e to ebc3650 Compare September 15, 2026 10:11
…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 (`Map<Method, Object>`) to `Tool` definition.
- Add validation in `AfcUtil.getFunctionMap(...)` to ensure that any non-static method has a corresponding instance in `functionInstances`, failing fast with a descriptive `IllegalArgumentException`.
- Update `AfcUtil` to extract function instances from `GenerateContentConfig` and invoke target methods with their bound instance (`method.invoke(instance, args)`).
- Safely handle optional `name` and `args` in `FunctionCall`, preventing `NoSuchElementException` and handling null map entries in `AfcUtil.getFunctionResponseParts` and `getFunctionResponse`.
- Pass `functionInstanceMap` across synchronous and asynchronous model generation loops (`Models`, `AsyncModels`, `Chat`, and `AsyncChat`).
- Add unit tests in `FunctionDeclarationTest`, `AfcUtilTest`, and `PrivateChatAfcTest`.

Closes #807

PiperOrigin-RevId: 981705233
@copybara-service
copybara-service Bot merged commit a372539 into main Sep 15, 2026
9 checks passed
@copybara-service
copybara-service Bot deleted the copybara_964733917 branch September 15, 2026 10:30
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