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 what we'reworking with.
Assistant
This is the langchain4j repository. Let me explore the structure related to the @Tool annotation.
Assistant
Let me find the class that processes @Tool annotations - this is likely ToolSpecifications or similar.
Assistant
I found the issue. The
toolSpecificationsFrommethod usesgetDeclaredMethods(), which only returns methods declared in the specified class, not inherited methods. Let me check the existing tests:Assistant
Now let me find the code that invokes the tool methods - the issue mentions that inherited tools weren't being called.
Assistant
Let me find where
toolSpecificationsFromis called tounderstand the full flow.Assistant
Now let me look at how the tools are executed. The issue mentions that inherited tools weren't being called. Let me check the tool execution path.
Assistant
I found another bug in
DefaultToolExecutor.findMethod- it usesgetDeclaredMethods()instead ofgetMethods()to find methods. Let me also check where tools are scanned/registered in the service layer:Assistant
Now I found another instance of the bug in
ToolService.findTools. It usesobjectWithTools.getClass().getDeclaredMethods(). Let me also checkgetAnnotatedMethodin Utils:Assistant
Let me also look at the AgenticServices.java:
Assistant
Let me reproduce the bug first to confirm my understanding:
Assistant
Now let me write a reproduction script to verify the bug:
Assistant
Let me check if there are existing utilities for walking class hierarchies:
Assistant
Let me search for existing patterns of class hierarchy traversal:
Assistant
Now I have a clear understanding. Let me look at how the AiServices handles tools to understand the full flow:
Assistant
Let me run the existing tests first to make sure they pass before any changes:
Assistant
Tests pass. Now let me create a temporary reproduction script to confirm the bug:
Assistant
Bug confirmed! Now let me design the fix. Let me first look at the Utils.java to find a good place to add a utility:
Assistant
Let me check existing behavior with non-public @Tool methods:
Assistant
Let me verify Java's behavior with
getMethods()and annotation inheritance:Assistant
Now I understand the Java behavior. Let me implement the fix. I'll modify three files:
Assistant
Now let me make the fix to
ToolSpecifications.java:Assistant
Let me continue with making the fix. I need to usea tool call to modify the file.
Assistant
Now let me edit the
ToolSpecifications.javato walk the class hierarchy:Assistant
Let me use sed to make the edit:
Assistant
Let me edit the file using sed to replace the method:
Assistant
Now let me insert the new code at line 62:
Assistant
Now let me clean up the unused import:
Assistant
The static import
Arrays.streamis no longer used since we removedstream(classWithTools.getDeclaredMethods()). ThetoListis still being used. Let meremove the unused import.Assistant
Now let me check the file compiles correctly:
Assistant
Wait, let me check that the build was successful with my changes. The output looks like it just compiled without errors, but let me confirm it actuallyrecompiled ToolSpecifications.java:
Assistant
Build succeeded. Now let me run the reproduction script to verify the fix:
Assistant
Now let me check
ToolService.findToolsand fix it too:Assistant
Let me check if
DefaultToolExecutor(Object, ToolExecutionRequest)is used in production:Assistant
Now let me add a utility method to Utils.java that can be reused, and then update the three places:
Assistant
Let me add a utility method to Utils.java that can be reused:
Assistant
Good - all the imports I need are already there. Let me add the utility method after
getAnnotatedMethod:Assistant
HashSet is not imported. Let me add the import:
Assistant
Now let me insert the new utility method after
getAnnotatedMethod:Assistant
I see there's an extra
}. Let me check the area and fix:Assistant
I see there's an extra
}. Let me fix it:Assistant
Let me look at the full content with line numbers:
Assistant
Now I need to add the closing
}afterreturn Optional.empty();:Assistant
Now I need to remove the extra
}on line 600:Assistant
Now let me compile to verify:
Assistant
Now let me update
ToolSpecifications.javato use this new utility:Assistant
Now let me replace the local implementation with a call to the utility method:
Assistant
The imports should be sorted alphabetically. Let me fix that:
Assistant
Hmm, sed
aappends AFTER the line, so my approach actually puts iton the wrong line. Let me look:Assistant
The imports are correctly ordered alphabetically. Now let me replace the local implementation:
Assistant
Now let me delete the local implementation and update the call:
Assistant
Now let me update the
toolSpecificationsFrommethod to use the utility:Assistant
Let me check the unused imports:
Assistant
HashSet is still used elsewhere. Let me check Method:
Assistant
Method is still used elsewhere. Let me compile:
Assistant
Now let me run the existing tests to make sure nothing is broken:
Assistant
All existing tests pass. Now let me run the reproduction script:
Assistant
Now let me fix
ToolService.findToolsandDefaultToolExecutor.findMethod:Assistant
Let me update the
ToolService.findToolsmethod:Assistant
Now let me update the
findToolsmethod:Assistant
Oh no, I removed too much. Let me see the original and fix this:
Assistant
Now let me also remove the unused
getAnnotatedMethodimport:Assistant
Good, the unused import is gone. Now let me compile:
Assistant
I need to rebuild langchain4j-core first since langchain4j depends on it. Let me install langchain4j-core:
Assistant
Now let me also fix
DefaultToolExecutor.findMethod:Assistant
Now let me update the
findMethodin DefaultToolExecutor:Assistant
Let me add a blank line:
Assistant
Now let me compile and run the reproduction:
Assistant
All three tests pass now. Let me run the existing tests to make sure nothing is broken:
Assistant
Let me also run other relevant tests:
Assistant
Let me also check if there are tests for AI services with tools:
Assistant
Let me write a more comprehensive test to verify edge cases:
Assistant
Let me verify edge case 3 with actual execution:
Assistant
Now let me check if there are other tests that might be affected. Let me look at agent tests:
Assistant
All 217 tests pass! Let me also run tests in langchain4j-core that might be relevant:
Assistant
All tests pass. Now let me also think about the issue mentioned in the bug report - it says this should also work in LC4J. Let me check if there are similar patterns in other modules:
Assistant
Let me also check the DeclarativeUtil file:
Assistant
Let me also test the scenario where parent and child have different annotations for the same method signature:
Assistant
Let me also test a final method case:
Assistant
Let me run all tool-related tests in langchain4j-core to make sure nothing is broken:
Assistant
Let me test private @Tool methods:
Assistant
Let me also test protected methods:
Assistant
Let me also test interface scenarios:
Assistant
The bug is specifically about class inheritance, so I'll focus on that. Let me verify my final code:
Assistant
There's a missing blank line between the
getAnnotatedMethodmethod ending and the new method. Let me fix this:Assistant
Let me also verify the ToolService.java file has a clean structure (I might have removeda blank line accidentally):
Assistant
Let me clean up the blank line correctly:
Assistant
Oops, I deleted the
forline. Let me fix this by re-adding it:Assistant
Hmm, the
forline is on the wrong line. Let me see the entirestructure better:Assistant
The structure is OK, just there's an extra blank line on line 229. Let me fix it.
Assistant
There's a missing blank line between line 226 and 227. Let me fix:
Assistant
Let me check if there are unused Optional imports:
Assistant
Optional is still used elsewhere. Now let me compile and re-run tests:
Assistant
Let me run all the tool-related tests once more:
Assistant
All 369 tests pass (152 are skipped which is expected for IT tests). Let me also run my reproduction script:
Assistant
Let me run all my comprehensive edge case tests:
Assistant
All my edge case tests pass. Let me run all the langchain4j-core tests to make sure my code is fully compatible:
Assistant
All 1088 langchain4j-core tests pass. Let me also run all tests in the langchain4j module:
Assistant
All 1383 tests pass! Let me also clean up the temporary reproduction files:
Assistant
Let me check the final diff to make sure my changes are clean:
Assistant
Let me also verify the final state of each file:
Assistant
Let me run all the relevant tests one more time to make sure nothing is broken:
Assistant
All tests pass. Let me also recreate the reproduction script and verify it one more time:
Assistant
Let me clean up the verification directory:
Assistant
Let me also verify the final state of the modified files is clean:
Assistant
Let me do a final test run to confirm everything is working:
Assistant
All tests pass. Let me review the final summary of my changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.