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
Good, both modules use
dev.langchain4j.internal. Let's add a helper methodgetAllMethodsin the coreUtils.java(sinceToolSpecificationsis in langchain4j-core), and reuse it fromlangchain4jmodule too.Assistant
Now let's add a helper method
getAllMethods(Class<?> clazz)that walks up the class hierarchy, excluding overridden duplicates (by method signature), similar toClass.getMethods()but including non-public methods too.Assistant
Now insert a new helper method after
getAnnotatedMethod(line 553), before the javadoc forwarnIfNullOrBlank.Assistant
Need to add a blank line before line 580's javadoc, and add
Arraysimport.Assistant
Now add the
ArraysandHashSetimports (need to check if HashSet is already imported).Assistant
Also
ArraysbeforeArrayListalphabetically - "Arrays" vs "ArrayList": 'ArrayL' < 'Arrays' alphabetically since 'L' < 's'. Let's fix ordering.Assistant
Now let's verify the full method body once more and check for compile correctness (open file to view).
Assistant
Now update
ToolSpecifications.javato usegetAllMethodsinstead ofclassWithTools.getDeclaredMethods().Assistant
Now add the
Utilsimport to this file.Assistant
streamstatic import is no longer used, but that's just an unused import warning, not error. Let's remove it for cleanliness.Assistant
Now let's fix
ToolService.javafindToolsmethod andDefaultToolExecutor.java'sfindMethod.Assistant
Now replace the fully qualified usage with the static import call.
Assistant
Good, no more occurrences. Now let's check earlier code around line 150-200 for other tool scanning logic (like
configureTools).Assistant
Now
tools(Collection<Object> objectsWithTools)callsfindToolswhich we already fixed. Let's now fixDefaultToolExecutor.findMethod.Assistant
Fix ordering (getAllMethods should come before getOrDefault alphabetically).
Assistant
Now update the
findMethodmethod.Assistant
Good, we've covered the two main scanning locations. Now let's check the
getMethodsusage inToolSpecifications.javafortoolExecutorForor other methods that might also scan by class.Assistant
Now let's compile the project to make sure everything builds correctly.
Assistant
Compilation succeeds. Now let's write a quick test script to verify the fix works for inheritance.
Assistant
Now let's find the compiled classpath (jars) to compile & run this repro.
Assistant
Both
ToolSpecifications.toolSpecificationsFromandToolService.findToolsnow correctly discover inherited@Toolmethods. Let's also verify tool execution actually invokes the inherited method correctly (testDefaultToolExecutorend-to-end).Assistant
End-to-end tool execution for an inherited
@Toolmethod now works correctly. Let's now run the existing test suite for the affected modules to make sure nothing broke.Assistant
Network available now, so tests ran. Let's check whether they passed.
Assistant
All passing. Now run tests for
langchain4jmodule (ToolServiceTest, DefaultToolExecutor tests, AiServices tests related to tools).Assistant
Now rerun the langchain4j module tests.
Assistant
All passing now. Let's check the surefire reports summary explicitly.
Assistant
All good. Now let's run broader tests related to
AiServicestool functionality to catch any regressions.Assistant
All passing. Let's also run the full core module test suite to be safe (excluding possibly slow/network tests) — but might take long. Let's just run relevant related tests broadly with a keyword filter.
Assistant
All Tool-related tests in
langchain4j-corepass too. Now let's also run the fulllangchain4jmodule*Tool*test filter for completeness.Assistant
All pass. Now let's double check the whole diff for cleanliness and remove the temporary repro files.
Assistant
Compiles fine. Let's view the final diff once more for review.
Assistant
The change set is minimal and focused. Let's go through the checklist:
Assistant
This looks good. Let me review the checklist:
- I have successfully reproduced the reported error: Yes — I wrote a reproduction script (
Repro.java) using a class with@Toolmethods in a superclass and a subclass. Before the fix,ToolSpecifications.toolSpecificationsFrom()andToolService.findTools()only found the subclass's own@Toolmethods (viagetDeclaredMethods()), missing the inherited one, confirming the bug.
- I have successfully reproduced the reported error: Yes — I wrote a reproduction script (
Assistant
Need slf4j on classpath. Let me add the earlier collected classpath dependencies.
Assistant
Confirmed: before the fix, only 1 (the subclass method) is found — the inherited
addtool is missing. Now restore the fix and verify again.Assistant
After the fix, both
subtract(declared in subclass) andadd(inherited from superclass) are found. This confirms the fix resolves the issue.Assistant
All tool-related tests pass with no failures/errors. Let's also run the core module
*Tool*tests once more for final confirmation.Assistant
All good. Let me review the checklist:
- [x] If an error was reported in the issue description, I have successfully reproduced it. I wrote a reproduction script that demonstrated
ToolSpecifications.toolSpecificationsFrom()only picked up@Toolmethods declared directly in a class, missing methods inherited from a superclass — matching the exact bug described (inherited@Toolmethods not found/called). - [x] If an error was reported in the issue description, I have confirmed that my fix resolves the error. After applying the fix (new helper used in , , and ), the reproduction script found both the inherited and the subclass's own methods, and an end-to-end execution test confirmed that an inherited method can actually be invoked correctly via .
- [x] If an error was reported in the issue description, I have successfully reproduced it. I wrote a reproduction script that demonstrated
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.