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
I'll start by exploring the repository structure to understand how it's organized and find the relevant code for the
@Toolannotation.Assistant
Let me look for the @Tool annotation and related code.
Assistant
Let me explore the repository structure and understand how @Tool methods are discovered.
Assistant
Now I need to understand how the @Tool methods are discovered and registered. Let me search for the code that processes these annotations.
Assistant
Let me look at the ToolSpecifications class since that seems to be where tools are discovered and processed.
Assistant
I can see the issue. In
ToolSpecifications.toolSpecificationsFrom(Class<?> classWithTools), it usesgetDeclaredMethods()which only returns methods declared directly in the class, NOT methods from superclasses. Let me explore further to understand how tools are discovered.Assistant
Let me look at the test file to understand how tools are expected to work, and also check if there are any tests for inheritance.
Assistant
Now let me look at the AbstractAiServiceIT test which might have inheritance-related tests:
Assistant
Let me explore how tools are discovered in the AiServices code:
Assistant
Let me examine the ToolService to understand how tools are discovered:
Assistant
Now I understand the issue. The
@Toolmethods are not being inherited from superclasses because bothToolSpecifications.toolSpecificationsFrom()andToolService.findTools()usegetDeclaredMethods()which only returns methods declared directly in the class, not inherited ones.Let me first write a test to reproduce this issue:
Assistant
Let me explore the project structure more to understand the build system and how to run tests.
Assistant
Let me continue exploring and create a reproduction test. First, let me understand the directory structure better:
Assistant
Now I'll create a test script to reproduce the bug before fixing it:
Assistant
Let me first check if maven is available and compile the core module, then compile the test:
Assistant
Let me compile the langchain4j-core module first, then compile and run the test:
Assistant
Good, the core module compiled. Now let me compile and run the test to reproduce the bug.
Assistant
The bug is confirmed. The
ToolSpecifications.toolSpecificationsFrom()method only finds 0 tools instead of 2 because it usesgetDeclaredMethods()which doesn't include inherited methods.Now let me also check how
ToolService.findTools()handles this, since the issue description mentions it should be fixed there too:Assistant
Let me compile the langchain4j module first and then run the test for ToolService.
Assistant
Both bugs are confirmed. Now I understand the issue clearly:
ToolSpecifications.toolSpecificationsFrom()usesgetDeclaredMethods()which only returns methods declared directly in the class, not inherited ones.ToolService.findTools()also usesgetDeclaredMethods()for the same problem.
Let me fix both files:
Assistant
I need to fix the
ToolSpecifications.toolSpecificationsFrom(Class<?> classWithTools)method to include methods from superclass hierarchy. I'll need to traverse the class hierarchy and collect all methods annotated with@Tool.Let me look at the current implementation more carefully. The method currently uses
getDeclaredMethods()which only gets methods declared directly in the class. I need to change it to also traverse the superclass hierarchy.The key changes needed:
- In
ToolSpecifications.toolSpecificationsFrom(Class<?>)- traverse the entire class hierarchy - In
ToolService.findTools(Object)- traverse the entire class hierarchy
Let me also check if there are any other places that use
getDeclaredMethods()for tool discovery.- In
Assistant
Now let me fix the
ToolSpecifications.toolSpecificationsFrom(Class<?>)method to traverse the class hierarchy:Assistant
I need to use the correct edit command format. Let me try again with the proper syntax.
Assistant
Let me try with sed or a different approach.
Assistant
Let me try using the
editcommand with the correct heredoc syntax:Assistant
Let me try a different approach - I'll use the edit command with heredoc syntax properly.
Assistant
The edit command is having trouble parsing. Let me use sed to make the replacement:
Assistant
Let me try a different approach using sed to modify the file:
Assistant
Let me try using the bash approach with a Java replacement script:
Assistant
Let me verify the fix was applied correctly:
Assistant
Good, the first part is fixed. Now I need to:
- Update the Javadoc comment
- Fix the
ToolService.findTools()method in langchain4j module - Recompile and test
Let me update the Javadoc and then fix
ToolService.findTools():Assistant
Now let me fix the
ToolService.findTools()method:Assistant
Let me see the findTools method in ToolService.java to understand its structure before fixing it.
Assistant
The issue is clear now. I need to fix the
ToolService.findTools()method to traverse the class hierarchy and find @Tool methods from superclass as well.Looking at the current implementation, it uses
getDeclaredMethods()which only returns methods declared directly in the class, not inherited methods. I need to change it to traverse the entire class hierarchy similar to what I did forToolSpecifications.toolSpecificationsFrom().Let me fix the
ToolService.findTools()method.Assistant
Let me verify the ToolService change:
Assistant
The changes look correct. Now let me recompile the modules and test:
Assistant
The issue description mentions fixing this in "LC4J too if it already doesn't work there." Let me check if there's a separate LC4J module. The repository seems to already be langchain4j (LC4J). Let me also check the langchain4j-agentic module since it was mentioned in the search results.
Let me first compile and test the fix to make sure it works, then check if there's anything else to fix in the agentic module.
Assistant
Now let me recompile and run my test to verify the fix works:
Assistant
I need to include the dependencies. Let me build with maven and test properly.
Assistant
The compilation is successful. Now let me test my fix. I need to compile the test file with the proper classpath.
Assistant
Excellent! The
ToolSpecificationsfix is working. Now let me test theToolService.findTools()fix:Assistant
Both fixes are working. Now let me check if the langchain4j-agentic module also needs fixing:
Assistant
Let me check the langchain4j-agentic module more thoroughly:
Assistant
Let me check the context around line 741 in AgenticServices.java:
Assistant
This is about
@Agentannotation (a different feature), not@Tool. The agentic module doesn't use @Tool in the same way. Let me now run the existing tests to make sure the fix doesn't break anything.Assistant
I see the issue. The tests are failing because my implementation deduplicates using the tool name in a Map before validation happens. This means duplicate tool names within the same class aren't detected by
validateSpecifications.I need to fix this properly - duplicates within the same class should still be caught, but I should allow child classes to override parent class methods with the same name.
Let me look at the failing tests:
Assistant
Assistant
I need to call the tool properly with the tool name and arguments.
Assistant
Now let me recompile and run the tests again:
Assistant
Good, it compiled. Now let me run the tests again.
Assistant
All tests passed! Now let me verify my custom test still works:
Assistant
Excellent! Now let me test the ToolService fix as well:
Assistant
Now I need to test the ToolService.findTools() fix. Let me run the test.
Assistant
Now let me test some edge cases to ensure the fix is robust:
Assistant
Let me run the edge case tests.
Assistant
All edge case tests pass. Now let me run the existing tests for the langchain4j module to make sure nothing is broken:
Assistant
All tests pass. Let me verify the final state of my changes by looking at the modified files:
Assistant
Let me view the full diff:
Assistant
The changes look correct. Let me run the existing tests to make sure nothing is broken:
Assistant
Let me check the test results more carefully:
Assistant
There seems to be a test failure. Let me check what's going on.
Assistant
All 1088 tests in langchain4j-core pass. Let me also run tests in the langchain4j module to ensure the ToolService changes don't break anything:
Assistant
Let me clean up the test files I created and do a final verification:
Assistant
Let me verify the final state of the changes:
Assistant
I have successfully fixed the bug where
@Toolmethods were not being inherited from superclasses. Let me provide a summary of the changes:Summary of Changes
Issue
The
@Toolannotation was not being inherited from superclass methods. This was because bothToolSpecifications.toolSpecificationsFrom()andToolService.findTools()usedClass.getDeclaredMethods(), which only returns methods declared directly in the class, not inherited methods.Fix
1.
langchain4j-core/src/main/java/dev/langchain4j/agent/tool/ToolSpecifications.javaModified
toolSpecificationsFrom(Class<?> classWithTools)to:Assistant
The user wants me to submit my changes since I've completed the fix. I've successfully:
- Fixed ToolSpecifications.toolSpecificationsFrom() to traverse class hierarchy
- Fixed ToolService.findTools() to traverse class hierarchy
- Verified all existing tests pass
- Tested edge cases including inheritance, overrides, and duplicate detection
I should now submit the changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.