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 start by exploring the repository structure to understand the codebase.
Assistant
This is a large multi-moduleproject. Let me find the
Toolannotation and related tool handling code.Assistant
Now I can see the issue. The
toolSpecificationsFrommethod usesgetDeclaredMethods()which only returns methods declared directly in the class, not inherited methods. Let me explore more.Assistant
I can see two main places where the issue exists:
langchain4j-core/src/main/java/dev/langchain4j/agent/tool/ToolSpecifications.javaline 63 - usesclassWithTools.getDeclaredMethods()instead of also considering inherited methods.langchain4j/src/main/java/dev/langchain4j/service/tool/ToolService.javaline 229 - also usesobjectWithTools.getClass().getDeclaredMethods().langchain4j/src/main/java/dev/langchain4j/service/tool/DefaultToolExecutor.javaline 71 -object.getClass().getDeclaredMethods()infindMethod.
Let me also check the agentic folder.
Assistant
That's for the AgenticServices. That's not the issue we need to fix. Let me look at the existing tests to see how the existing tool methodsare tested.
Assistant
Let me create a reproducer script to confirm the bug exists.
Assistant
Let me create a reproducer script to confirm the bug exists:
Assistant
The bug is reproduced. Now let me look at all the places that use
getDeclaredMethods()for finding tool methods and fix them.Assistant
Looking at the usages:
ToolSpecifications.java(line 63): This is used to find tool methods. This needs to be fixed.ToolService.java(line 229): This is used to find tool methods from the object's class. This needs to be fixed.DefaultToolExecutor.java(line 71): This is used to find a method by name. This needs to be fixed.AgenticServices.java(line 741): This is for Agent methods, not Tool methods. Different use case.
Let me look at the
Utilsclass to see if there are utility methods:Assistant
Let me look at the broader context to understand:
Assistant
Let me check if Spring's
ReflectionUtils.getDeclaredMethodsis used somewhere similar:Assistant
Now let me think about the fix. I need to:
- Walk the class hierarchy to collect all declared methods (including inherited ones).
- Apply this fix in
ToolSpecifications.java,ToolService.java, andDefaultToolExecutor.java.
Let me add a helper utility method first:
Assistant
Let me check if interfaces are commonly used with
@Tool:Assistant
Let me check the test setup:
Assistant
Now let me implement the fix. First, I'll add a helper method to
Utils.java:Assistant
Let me add the imports and the helper method to
Utils.java:Assistant
I need to add
ArraysandHashSetimports. Let me do it directly using sed:Assistant
Wait, that introduced a duplicate import. Let me check the file:
Assistant
Let me reorganize the imports in correct order:
Assistant
Good, now let me add the helper method:
Assistant
Now let me insert the helper method between line 555 and 557:
Assistant
Now let me update the three places. First,
ToolSpecifications.java:Assistant
Good. Now let's update
ToolService.java:Assistant
Now let's update
DefaultToolExecutor.java:Assistant
All three places are fixed. Now let me compile to verify the changes:
Assistant
Now let me re-run the reproducer to verify the fix:
Assistant
The fix works. Let me also test edge cases:
Assistant
Let me also check if interfaces are commonly used with @Tool:
Assistant
Let me run the existing tests to make sure my changes don't break anything:
Assistant
All 18 existing tests pass. Now let me also run the related tests in the langchain4j module:
Assistant
All 68 tests pass. Now let me also clean up the test files:
Assistant
Let me also verify the test for inheritance scenario works. Let me create a temporary verification script:
Assistant
All my fix tests pass. Now let me clean up and check the code is well-formed:
Assistant
Let me look at the git diff to review the changes:
Assistant
Let me run a broader test to make sure nothing else is broken:
Assistant
Excellent! All 369 tests pass. Let me also check for related test files that might be affected:
Assistant
All 1129 tests pass. Let me also verify the changes don't break the langchain4j-core tests:
Assistant
All tests pass. Let me also check if the kotlin module has similar logic:
Assistant
Let me also check the agentic module to see if it has similar logic:
Assistant
Good - the agentic module doesn't use
@Tooldirectly, justToolProviderSupplieretc. My fix doesn't need to be applied there.Let me also look at other modules that might use @Tool. Let me see if there's any usage of @Tool ortoolSpecificationsFrom in other modules.
Assistant
Let me see if the google-ai-gemini or any other modules use a similar pattern for finding @Tool methods.
Assistant
Let me also verify any other places where getDeclaredMethods might be related to tool discovery:
Assistant
Let me check the AgenticServices code to understand whether it's related to the bug:
Assistant
Let me check the existing code for handling synthetic/bridge methods:
Assistant
No filtering for synthetic/bridge methods. So my helper doesn't need to worry about them either. Let me also write a final verification test to make sure everything works as expected:
Assistant
All tests pass except Test 5, where my expected value was wrong. The fix returns the string directly without JSON wrapping:
Assistant
The text looks correct now. Let me re-compile andrun again.
Assistant
All tests pass. Let me clean up:
Assistant
Let me also verify the final state of the changes:
Assistant
Let me run all related tests once more to confirm everything is OK:
Assistant
All 369 tests pass. Let me also run the UtilsTest to make sure my changes to Utils don't break anything:
Assistant
All 46 UtilsTest tests pass. Let me also look at the full diff one more time to make sure everything is good:
Assistant
Let me also check the issue mentions "This should be done in LC4J too if it already doesn't work there." Let me check if LC4J (Kotlin module) has similar logic:
Assistant
The Kotlin module doesn't have its own tool discovery logic - it uses the Java-based
ToolSpecificationdirectly. So my fix in the Java code is sufficient to fix the Kotlin code as well.Let me also make sure the code doesn't have any potential issues with the changes I made. Let me run the full test suite for langchain4j-core and langchain4j one more time:
Assistant
All 1383 tests pass! Let me also check my final changes one more time:
Assistant
Now let me address the checklist:
Checklist:
-
If an error was reported in the issue description, I have successfully reproduced it. ✓
- I created a reproducer script that confirmed
@Toolmethods defined in a parent class were not being picked up when the instance was a subclass.
- I created a reproducer script that confirmed
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error. ✓
- I verified the fix works for:
- Simple inheritance (BaseTools → DerivedTools) - finds 2 tools
- Multi-level inheritance (BaseTools → DerivedTools → DeepDerived) - finds 3 tools
- I verified the fix works for:
-
Assistant
All 1383 tests pass. Let me do one last review:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.