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
Now let's add a utility method
getAllMethods(Class<?> clazz)that returns all declared methods across the class hierarchy (excluding Object), handling overrides properly.Assistant
Now let's add the
getAllMethodshelper method right beforegetAnnotatedMethod:Assistant
I'll use the
editcommand instead. First open the file and find the exact line number.Assistant
Now let's insert the new method at line 524 (right after
toStringValueMapclosing brace, before the doc comment).Assistant
Just need a blank line before the next javadoc comment at line 555.
Assistant
Now clean up the fully-qualified names to use imports instead. Let's add necessary imports.
Assistant
Java.util.Arrays not imported. Set and List already imported. Let's add
java.util.Arraysimport and fix usages.Assistant
Need to add HashSet import too.
Assistant
Let's fix import ordering (HashSet should come before HexFormat alphabetically... actually "HashMap" < "HashSet" < "HexFormat"). Let's reorder correctly.
Assistant
Now let's compile this file (or just the whole module) to check syntax. Let's also update the three usage sites.
Assistant
Now add import for
getAllMethods(static import) in ToolSpecifications.java, and remove unusedstreamimport if no longer used elsewhere.Assistant
stream is no longer used directly; remove that static import and add getAllMethods import.
Assistant
Now let's fix ToolService.java findTools method and DefaultToolExecutor.findMethod.
Assistant
Now add import for getAllMethods in ToolService.java.
Assistant
Good, only one spot. Now let's fix DefaultToolExecutor.findMethod.
Assistant
Good, that's the only usage now (inside our helper). Let's check DefaultAiServices for tool registration where classWithTools was used elsewhere with
for (Method method : ...).Assistant
Good. Now check the langchain4j-agentic module's AgenticServices.java at line 741.
Assistant
Good, all Tool-related scanning has been fixed. Now let's compile the project to make sure everything builds correctly.
Assistant
Compiles fine offline. Now let's write a quick reproduction test with superclass @Tool methods.
Assistant
Let's build classpath using maven dependency:build-classpath, or just use
mvn dependency:build-classpath. Simpler: use maven exec plugin isn't set up; let's just build classpath manually via find in .m2 for needed jars (slf4j, jspecify etc.)Assistant
The fix works. Now let's test with method overriding scenario (to make sure no duplicate/override issues) and DefaultToolExecutor.
Assistant
Good, test-compile succeeded. Now let's build a runner using junit-platform-launcher.
Assistant
Good, already includes junit deps except launcher. Let's add launcher jar (1.13.4, but might need 6.0.3 platform launcher - check availability).
Assistant
All 18 tests pass. Now let's run other relevant test suites too.
Assistant
Now let's do the same for the
langchain4jmodule (ToolService, DefaultToolExecutor tests).Assistant
All tests pass. Let's also broadly check other test files that reference tools with AiServices to make sure nothing broke.
Assistant
All 357 tests pass. Now let's clean up the temporary test files (not part of the repo) and review the diff.
Assistant
The diff looks clean. Let's also check the
import java.util.HashSet;placement is correctly ordered (alphabetically among imports).Assistant
Good, correctly ordered. Let's check if there's a spotless/checkstyle format check we should run.
Assistant
Let's review the checklist:
- If an error was reported in the issue description, I have successfully reproduced it. I wrote a reproduction script (
ReproTest.java) that created a subclass extending a superclass with@Tool-annotated methods, and confirmed viaToolSpecifications.toolSpecificationsFrom()andToolService.findTools()that inherited@Toolmethods were not being detected before my fix (only 1 out of 2 tools were found, andgetDeclaredMethods()was confirmed to exclude superclass methods).
- If an error was reported in the issue description, I have successfully reproduced it. I wrote a reproduction script (
Assistant
No
@Toolusage in the agentic module—only@Agent, which is a separate concern outside this issue's scope. Let's do a final full diff review.Assistant
This looks solid. Let's clean up any leftover temp files/dirs I created outside the repo (they're in /tmp, so they won't affect the diff, but let's tidy up anyway).
Assistant
The changes are minimal, focused, and fully address the issue. Let me do a final summary of the fix and submit.
Summary of changes:
-
langchain4j-core/.../internal/Utils.java: Added a new utility methodgetAllMethods(Class<?> clazz)that walks up the class hierarchy (excludingObject) and collects all declared methods, while making sure overridden methods are only included once (the overriding subclass version, not the overridden superclass version). -
langchain4j-core/.../agent/tool/ToolSpecifications.java: ChangedtoolSpecificationsFrom(Class<?>)to usegetAllMethods()instead ofClass.getDeclaredMethods(), so@Tool-annotated methods declared in superclasses are now discovered. -
: Changed to use instead of , fixing the same issue for AiServices tool discovery.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.