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 the relevant code for
@Toolannotation processing.Assistant
Let me find the code that processes
@Toolannotations - the DefaultToolExecutor and related classes.Assistant
I can see the issue. The
toolSpecificationsFrom(Class<?> classWithTools)method usesclassWithTools.getDeclaredMethods()which only returns methods declared in that specific class, not inherited methods. To get inherited methods, we should useclassWithTools.getMethods()which returns all public methods including inherited ones, or we need to walk up the class hierarchy.However, there's a subtlety:
getMethods()returns all public methods (including inherited), but@Toolmethods might not be public. Let me check the DefaultToolExecutor and how it invokes methods.Let me also look at ToolService to understand the full picture.
Assistant
Found the key issues. Let me look at the
getAnnotatedMethodhelper in Utils and the DefaultToolExecutor.Assistant
Let me look at the DefaultToolExecutor to understand how methods are invoked.
Assistant
Let me check the
@Toolannotation definition to understand if it's marked@Inherited.Assistant
The
@Toolannotation is NOT marked@Inherited, so method-level annotations don't carry over. The core issue is thatgetDeclaredMethods()only returns methods declared in the specific class, not inherited ones. Let me look at existing tests around tool inheritance.Assistant
Let me check the
AbstractAiServiceIT.javafor tool-related test patterns.Assistant
Let me check if there's an existing utility for walking class hierarchies and look at the git history/blame for context.
Assistant
Let me write a reproduction test to confirm the bug. First, let me check the existing ToolService tests.
Assistant
Let me write a reproduction test to confirm the bug before making changes.
Assistant
Confirmed the bug. The superclass's
superToolis not found. Now let me implement the fix. Let me check usages of theDefaultToolExecutor(Object, ToolExecutionRequest)constructor andfindMethod.Assistant
The
DefaultToolExecutor(Object object, ToolExecutionRequest toolExecutionRequest)constructor usesfindMethodwhich usesgetDeclaredMethods(). Let me look at the DefaultToolExecutorTest cases at lines 362, 378, 405 to understand the findMethod usage.Assistant
Let me look at the imports in ToolSpecifications.
Assistant
Now let me implement the fix. First, the
ToolSpecificationshelper method and refactortoolSpecificationsFrom(Class).Assistant
Now let me edit
ToolSpecifications.java. I'll:- Add imports for
ArraysandgetAnnotatedMethod. - Add the
toolMethodsFrom(Class<?>)helper. - Refactor
toolSpecificationsFrom(Class<?>)to use it.
Let me edit the imports first.
- Add imports for
Assistant
Let me use the
editcommand instead.Assistant
Now let me add the
java.util.Arraysimport and refactortoolSpecificationsFrom(Class)to use a new helper methodtoolMethodsFrom.Assistant
Now let me refactor
toolSpecificationsFrom(Class)and add thetoolMethodsFromhelper method.Assistant
Now let me run the reproduction test to confirm the fix for
toolSpecificationsFrom.Assistant
Both tools are now found. Now let me update
ToolService.findToolsto use the new helper method.Assistant
Now let me update
findToolsto use the newtoolMethodsFromhelper.Assistant
My edit left duplicate leftover lines. Let me remove the dangling duplicate lines 243-248.
Assistant
Now I need to add the import for
toolMethodsFromand check ifgetAnnotatedMethodis still used in ToolService.Assistant
getAnnotatedMethodis now unused. Let me remove that import and add thetoolMethodsFromimport.Assistant
Now remove the unused
getAnnotatedMethodimport.Assistant
Now let me update
DefaultToolExecutor.findMethodto also walk the class hierarchy.Assistant
The edit went to the wrong file — it edited
ToolService.javainstead ofDefaultToolExecutor.javabecause that's the currently open file. Let me revertToolService.javaand redo properly.Assistant
Now let me re-apply the
DefaultToolExecutor.findMethodchange using the--fileoption explicitly.Assistant
Let me use the
editcommand with the--fileoption to targetDefaultToolExecutor.javaspecifically.Assistant
There's an extra
}at line 88. Let me remove it.Assistant
Now I need to re-apply the
ToolService.javachanges (which were reverted). Let me redo them carefully using--file. First the imports.Assistant
Now remove the now-unused
getAnnotatedMethodimport (line 9).Assistant
Now let me update the
findToolsmethod body.Assistant
There are leftover closing braces on lines 236-237. Let me remove them.
Assistant
Now
findToolslooks good. Let me check ifOptionalis still used elsewhere in ToolService (since I removed theOptional<Method>usage).Assistant
Optionalis still used. Now let me compile and run the existingToolSpecificationstests plus my repro test.Assistant
Both tests pass (no failures shown). Let me confirm the build summary.
Assistant
Now let me install
langchain4j-corelocally, then compile and test thelangchain4jmodule (ToolService and DefaultToolExecutor tests).Assistant
The javadoc plugin is failing on warnings. Let me skip javadoc generation during install.
Assistant
The jacoco coverage check fails during install. Let me skip jacoco too. Actually, I can just skip the jacoco check with
-Djacoco.skip=true.Assistant
langchain4j-core installed. Now let me compile and test the
langchain4jmodule's tool tests.Assistant
All tests pass. Now let me write a more comprehensive temporary test in the
langchain4jmodule to verify the full flow throughToolService.findToolsand actual execution of inherited tools (including edge cases). I'll remove it before submitting.Assistant
All 5 tests pass, including edge cases. Now let me search for other places in the codebase that scan for
@Toolmethods usinggetDeclaredMethods, which may need the same fix (e.g. other modules).Assistant
Let me check the
AgenticServices.javausage at line 741, which may also scan for@Toolmethods.Assistant
This
nonAiAgentMethodscans for@Agent(a different annotation), not@Tool, so it's out of scope for this issue. Let me checkAiServiceValidationand any other place that builds tool specs/executors.Assistant
Let me check
AiServiceValidation.javawhich referenced@Tool.Assistant
This is AI service interface validation, not tool scanning. Out of scope. Now let me run the broader tool-related test packages to ensure nothing broke.
Assistant
The pattern with package needs a different syntax. Let me use
-Dtest='ToolServiceTest,DefaultToolExecutorTest,ToolExecutorTest,ToolExecutionTest,ToolExecutionResultTest,InheritanceToolsTest,HallucinatedToolNameStrategyTest,ProgrammaticCreatedImmediateReturnToolTest,StreamingProgrammaticCreatedImmediateReturnToolTest,ToolExecutionRequestUtilTest,LazyCalculationIntegrationTest'.Assistant
All tests pass. Let me also run the AiServices tests that exercise tool usage with mocked models (non-IT). Let me find them.
Assistant
All pass. Let me check if the
langchain4j-kotlinmodule has its own@Toolscanning that needs fixing.Assistant
No separate scanning in
langchain4j-kotlin. Now let me remove the temporary test files I created (per instructions, I should not add new tests).Assistant
Let me review the final diff to make sure everything is clean.
Assistant
The diff looks clean. Let me restore the removed blank line in
ToolServiceto keep the diff minimal.Assistant
Now let me run the relevant tests once more to confirm everything compiles and passes after the blank line restoration.
Assistant
Let me explicitly run the core
ToolSpecificationsTestonce more to be 100% sure.Assistant
Let me check whether any existing tests extend a class with
@Toolmethods, which might now produce additional specs and break assertions.Assistant
Let me check
AiServicesObservabilityTestsandAiServicesWithToolErrorHandlerTestfor any class hierarchy with@Toolthat could now produce extra specs.Assistant
The
@Toolis at line 639. Theextendshere are listener classes (not @Tool tool classes). The tool class with @Tool (line 639) — let me check whether it extends a class that also has @Tool. Let me look at the context around 639.Assistant
Calculatordoesn't extend a@Toolclass, so no impact. Let me runAiServicesObservabilityTeststo be safe.Assistant
Let me verify no test tool class extends another
@Tool-bearing class (which could change spec counts). Let me search across all test files forextendscombined with@Toolin the same class.Assistant
Let me do a more targeted check for any test tool class that extends a superclass.
Assistant
None of these extend a
@Tool-bearing class, so my change won't introduce unexpected extra tools for existing test tool classes.Let me do a final review of the complete diff.
Assistant
The diff is clean. Let me verify there are no now-unused imports in
ToolSpecifications(e.g.,stream).Assistant
The static import
import static java.util.Arrays.stream;may now be unused (only.stream()instance method is used). Let me check.Assistant
The static
Arrays.streamimport is now unused. Let me remove it to keep the code clean.Assistant
Let me re-run the core
ToolSpecificationsTestto confirm everything still compiles and passes after removing the unused import.Assistant
Let me reinstall core and re-run the langchain4j tool tests once more to be fully sure.
Assistant
All tests pass. Let me confirm the final git status (no leftover temp files).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.