ISSUE DESCRIPTION [BUG] @Tool methods not inherited from superclas @Tool methods not…
- User
ISSUE DESCRIPTION
[BUG] @Tool methods not inherited from superclas
@Toolmethods not inherited from superclass. I think this is a bug as this is something that should work.I didn't 100% verify this, but my inherited tools weren't being called and the LLM was stating that it didn't know what tool to call. Once I put the tool methods back in the concrete class, things worked, so I'm assuming that this bug report is true.
This should be done in LC4J too if it already doesn't work there.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /langchain4j, current file: none) bash-$
Assistant
Let me explore the repository structure to understand the codebase and find where
@Toolmethods are discovered/processed.Assistant
Found it.
getDeclaredMethods()only returns methods declared in the class itself, not inherited methods. Let me look at the relevant code.Assistant
Now let me look at the DefaultToolExecutor.java around line 71.
Assistant
So there are two places where
getDeclaredMethods()is used:ToolService.findTools()- line 229 - only iterates declared methods, missing inherited@Toolmethods.DefaultToolExecutor.findMethod()- line 71 - only looks at declared methods for finding method by name.
The issue is that
@Toolmethods in a superclass aren't discovered. Let me think about the best fix.For
findTools, we should iterate over all methods including inherited ones. We could usegetMethods()which returns all public inherited methods, but@Toolmethods could be private/package-private too. ActuallygetMethods()returns all public methods (including inherited). But inherited methods from a superclass that are public would be returned by . However, returns all methods declared directly in the class (public, protected, private, package-private) but not inherited.Assistant
Now let me look at the existing tests for
DefaultToolExecutorTestandToolExecutorTest.Assistant
Let me write a quick reproduction test to confirm the issue. First, let me check the imports and structure of
ToolService.Assistant
Let me write a reproduction test to confirm the issue first.
Assistant
The project hasn't been built yet. Let me build the necessary modules.
Assistant
Build succeeded. Now let me compile and run my reproduction test.
Assistant
Jackson version mismatch. Let me find the right jackson version.
Assistant
The classpath had multiple jackson versions; an older one got picked first. Let me use the specific 2.21.3 versions.
Assistant
Confirmed the bug — only
childToolis found, the inheritedbaseToolis missing. Let me save the working classpath and check whereDefaultToolExecutor(Object, ToolExecutionRequest)(thefindMethodconstructor) is used.Assistant
The
DefaultToolExecutor(Object object, ToolExecutionRequest toolExecutionRequest)constructor andfindMethodis public API. Let me check if it's used in tests or elsewhere. Let me search broadly including tests.Assistant
Let me look at the
@Toolannotation definition and any validation about method visibility.Assistant
Let me check whether same-name overloaded
@Toolmethods are supported/tested.Assistant
Let me check how the tool name is derived in
ToolSpecifications.toolSpecificationFromand whether duplicate names would be an issue.Assistant
Let me check if there are existing tests using non-public (private/protected/package-private)
@Toolmethods, which would be affected by switching togetMethods().Assistant
Let me check the TestTool classes used in DefaultToolExecutorTest to see if they use non-public @Tool methods.
Assistant
Let me look at the imports and end of Utils.java to add the helper.
Assistant
Let me check if there are any existing tests with class hierarchies involving
@Toolmethods (to ensure my change won't break them).Assistant
extends InvocationParameters— not a tool hierarchy. Good. Let me check the other files briefly for actual tool class hierarchies.Assistant
No existing tests have a subclass extending a
@Tool-bearing class. Now let me implement the fix. First, add helper methods toUtils.java.Assistant
Now let me add the
HashSetimport and the helper methods. First the import:Assistant
Now let me add the helper methods after
getAnnotatedMethod(around line 554).Assistant
I need to add a blank line between the new method and the next javadoc.
Assistant
Now let me update
ToolService.findToolsto usegetAllMethods.Assistant
The replace command needs the file to be open / displayed. Let me open the file first.
Assistant
Now update the
findToolsloop. Let me view it again with current line numbers.Assistant
There's a duplicate
List<AiServiceTool> result = new ArrayList<>();line (229 and 230). Let me remove the duplicate.Assistant
Now let me update
DefaultToolExecutor.findMethodto walk the hierarchy too.Assistant
Let me check for other places across the whole repo that scan for
@Toolmethods usinggetDeclaredMethods.Assistant
There's also
ToolSpecifications.toolSpecificationsFrom(Class<?>)in core which usesgetDeclaredMethods(). This is public API and should be fixed for consistency too. Let me check its usages.Assistant
ToolSpecifications.toolSpecificationsFromis used in core tests and some module tests. Let me update it to usegetAllMethodsand skip bridges. Let me check the existing core tests to make sure I don't break them.Assistant
Now let me update
ToolSpecifications.toolSpecificationsFrom(Class<?>)in core to usegetAllMethods. Let me check its imports.Assistant
Let me check if the
streamstatic import is still used elsewhere in the file.Assistant
The
streamstatic import is now unused. Let me remove it to avoid an unused-import warning.Assistant
Let me also check the agentic module's
getDeclaredMethodsusage to see if it's related to@Tool.Assistant
The agentic one uses
@Agent, not@Tool— out of scope. Now let me rebuild and re-run the reproduction test.Assistant
The fix works — both
childTooland inheritedbaseToolare now found. Let me write a more comprehensive test covering edge cases: override with re-annotation, generics/bridge, package-private, deep hierarchy, and actual execution viaDefaultToolExecutor.Assistant
All edge cases pass. Now let me run the existing unit tests for the affected modules.
Assistant
The output is sparse. Let me run with a surefire summary. Let me check the result by reading the surefire reports or using -Dsurefire. Let me re-run with more output.
Assistant
Now let me run the relevant non-IT tests in the
langchain4jmodule (tool package + AiServices unit tests).Assistant
All relevant tests pass. Let me run the full
langchain4jmodule surefire (unit) test suite to be safe — ITs are typically run by failsafe, not surefire.Assistant
All 1383 unit tests pass. Let me also run the full
langchain4j-coreunit test suite since I changedToolSpecificationsandUtils.Assistant
Let me verify the
langchain4j-google-ai-geminiFunctionMapperTestclasses (which usetoolSpecificationsFrom) don't have inheritance that could be affected.Assistant
These classes have no
@Toolinheritance, so no behavior change.Importantly, my change is purely additive: for the override-without-annotation case, the old
getDeclaredMethods()-only behavior already removed the tool (the override isn't annotated), and my code keeps that same behavior — it only adds inherited non-overridden@Toolmethods. Let me review the final diff.Assistant
The diff is clean. Let me make sure there are no other
@Tooldiscovery sites in main source that I missed.Assistant
All
@Tooldiscovery sites are covered. Let me verifyfindToolsis indeed the entry point used when registering tool objects via.tools(...).Assistant
Confirmed
tools(Collection<Object>)→findToolsis the entry point for.tools(object), which my fix covers.Let me do a final cleanup check and confirm git status only contains intended changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.