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.
Assistant
Let me look for the code that discovers
@Toolmethods.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. We should usegetMethods()instead to include inherited methods, or iterate through the class hierarchy.But we need to be careful:
getMethods()returns all public methods including inherited ones. But@Toolmethods might not be public. Let me think about this.Actually,
getMethods()returns all public member methods (including inherited).getDeclaredMethods()returns all methods declared in the class (including private, protected, public) but not inherited.The proper solution would be to walk up the class hierarchy and collect methods annotated with
@Tool. Let me also look atDefaultToolExecutor.javaandToolService.javato see how methods are resolved for execution.Assistant
Now let me look at the ToolService.java to see how tools are registered.
Assistant
Let me look at
Utils.getAnnotatedMethodto understand what it does.Assistant
Now let me understand the issue and plan the fix. The issue is that
@Toolmethods defined in a superclass are not inherited by subclasses.The relevant places:
ToolSpecifications.toolSpecificationsFrom(Class<?> classWithTools)- usesclassWithTools.getDeclaredMethods()ToolService.findTools(Object objectWithTools)- usesobjectWithTools.getClass().getDeclaredMethods()DefaultToolExecutor.findMethod(Object object, ToolExecutionRequest toolExecutionRequest)- usesobject.getClass().getDeclaredMethods()
The fix should walk up the class hierarchy to find all
@Toolmethods.For #1 and #2, we need to collect methods from the class and all its superclasses (but not Object).
Let me think about edge cases:
- A subclass overrides a method that's annotated with
@Toolin the superclass. The overridden method in the subclass might or might not have the annotation. In Java, annotations are not inherited by default (unless@Inherited). Let me check if@Toolis marked@Inherited.
Assistant
Assistant
Let me look at the existing tests for ToolService.findTools to understand the testing conventions.
Assistant
Let me look at how
ToolSpecifications.toolSpecificationsFromis used elsewhere in the codebase (not just tests) to understand the impact.Assistant
Let me also check the
langchain4j-agenticmodule'sAgenticServices.javaaround line 741 to see if it also needs fixing.Assistant
Let me check where the
DefaultToolExecutor(Object, ToolExecutionRequest)constructor is used.Assistant
The
DefaultToolExecutor(Object, ToolExecutionRequest)constructor doesn't seem to be used in main code, only tested. Let me check more broadly if it's used anywhere (maybe in other modules).Assistant
Let me look at the existing tests to understand conventions and ensure my fix won't break them.
Assistant
Let me look at the existing tests that test tool discovery. Let me check ToolExecutorTest.
Assistant
Let me write a reproduction test to confirm the bug. First let me check the build system.
Assistant
Let me create a temporary reproduction test to confirm the bug.
Assistant
The build also compiles other modules in
-am. Let me only target the langchain4j module. The error is fromlangchain4j-internal-test-retrybecause the test pattern matches. Let me setfailIfNoSpecifiedTests=falseand target only the langchain4j module. Actually-ambuilds dependencies. The other modules also run surefire with that test filter. Let me restrict.Assistant
Bug confirmed. Both methods only find
subTool, not the inheritedsuperTool. Now let me check whether there's a visibility check for@Toolmethods before implementing the fix.Assistant
The code uses
setAccessible(true)when there's an IllegalAccessException. So@Toolmethods can be non-public. I must NOT usegetMethods()(which only returns public methods). I should walkgetDeclaredMethods()up the hierarchy.Now let me implement the fixes. Let me start with
ToolSpecifications.java.Assistant
Now let me edit
ToolSpecifications.javato add the hierarchy-walking helper and updatetoolSpecificationsFrom(Class).Assistant
I need to verify
Methodis imported in ToolSpecifications.java. Let me check the imports.Assistant
The imports are present. Let me check if
Arrays.stream(thestreamimport) is still used elsewhere in the file.Assistant
The
Arrays.streamstatic import is now unused. Let me remove it to keep things clean.Assistant
Now let me update
ToolService.findToolsto walk the class hierarchy.Assistant
Now let me rewrite
findToolsto walk the class hierarchy while preserving proxy support viagetAnnotatedMethod.Assistant
Let me verify the imports in ToolService include
Map,LinkedHashMap,Optional.Assistant
Now let me update
DefaultToolExecutor.findMethodto walk the class hierarchy.Assistant
Let me update
findMethodto walk the class hierarchy.Assistant
Now let me check if there are other places in the agentic module that discover
@Toolmethods viagetDeclaredMethods.Assistant
The agentic module relies on the core
ToolService.findTools, so the fix propagates. Now let me compile and run the reproduction test.Assistant
The reproduction test passes. Now let me add edge-case tests to verify override handling and actual execution.
Assistant
The execution returns "super:null" — the parameter name lookup failed. The parameter is named
argbut reflection may returnarg0if-parametersisn't enabled. Let me check the compiler config.Assistant
No
-parametersflag, so reflection returns synthetic names (arg0). Let me update my test to usearg0as the JSON key, matching the existing convention.Assistant
Still null. Let me check whether
-parametersis actually enabled somewhere, which would make reflection return the real namearginstead ofarg0.Assistant
Let me empirically determine the reflected parameter name by printing it.
Assistant
The parameter name is
arg0. So JSON keyarg0should match, but my test still got null. Let me verify the actual file content after the sed change.Assistant
The sed didn't match because the Java source has escaped quotes (
{\"arg\":\"x\"}). Let me fix directly.Assistant
All 6 tests pass, including the edge cases (override with/without re-annotation, virtual dispatch, and
DefaultToolExecutor.findMethodresolution). Now let me remove the temporary test files and run the existing tests in the affected areas.Assistant
Let me check the actual test results more carefully.
Assistant
All passing. Let me run a broader set of tool-related unit tests in the langchain4j module to check for regressions.
Assistant
Let me also run tests in the core module that might be affected, plus check there are no other usages of
getDeclaredMethodsfor tool discovery I might have missed.Assistant
Let me check
ToolSpecificationsTestto ensure my changes align with its expectations.Assistant
Let me look at the test helper classes (Wrapper, InvalidToolsWithDuplicateMethodNames, InvalidToolsWithDuplicateNames) to ensure inheritance doesn't change their expected behavior.
Assistant
These helper classes don't use inheritance, so behavior is unchanged. Let me run the full set of non-IT unit tests in the
service/toolpackage and related AiServices tests to ensure no regressions.Assistant
All pass. Let me verify the agentic module's tool discovery to see if it has its own
@Tooldiscovery path that might also need fixing.Assistant
The agentic module uses
getDeclaredMethods()for@Agentmethods (a different annotation), not@Tool. Let me verify how the agentic module registers@Toolmethods to confirm it goes through the fixedToolService/ToolSpecificationspath.Assistant
The agentic module registers
@Toolmethods viaaiServices.tools(...)→ToolService.findTools, so the fix propagates there too. Let me do a final review of all my changes.Assistant
The diff looks good. Let me verify no temp files remain and check git status.
Assistant
Only the three intended source files are modified. Let me check for other callers of
toolSpecificationsFromacross all modules that might be affected by the new inheritance behavior.Assistant
Only internal usages. Let me do a final clean compile of both affected modules to be 100% sure there are no compile errors.
Assistant
Compilation succeeds. Let me do one final verification run of the key unit test suites to confirm everything passes together.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.