ISSUE DESCRIPTION [FEATURE] support non Result return types for AiService when tool…
- User
ISSUE DESCRIPTION
[FEATURE] support non Result return types for AiService when tool execution is IMMEDIATE Is your feature request related to a problem? Please describe. If using ReturnBehavior.IMMEDIATE, your AiService method has to return Result even if all tools called return
voidor all returnnullor there's one tool method that returns same type as AiService return type.Describe the solution you'd like I'd like to support non-Result return types for AiService methods when tool execution has IMMEDIATE results based on these rules:
- If ai service method return type is void, just return null
- If any tool executions are TO_LLM throw exception (IMMEDIATE_IF_LAST case)
- If all tool executions have a null result and return type is nullable (not a primitive), then return null
- If there is one non-null tool execution result, and the result can be resolved to the return type, then return that object
I have a PR for this open in Quarkus too:
https://github.com/quarkiverse/quarkus-langchain4j/pull/2529
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.
Assistant
Let me start by understanding the repository structure and the issue. The issue is about supporting non-Result return types for AiService when tool execution is IMMEDIATE. Let me explore the repository first.
Let me understand the codebase structure and find the relevant files.
Assistant
Let me explore the repository structure and find the relevant code related to AiService and tool execution.
Assistant
Let me look at the key files to understand the current implementation
Assistant
Now let me look at AiServices.java - the main file that handles AI service execution
Assistant
Let me read the file in parts.
Assistant
Let me read more of the AiServices.java file, especially around where immediate return is handled.
Assistant
Let me look at ToolService.java - this seems to be where the immediate return logic is handled
Assistant
Let me read the full AiServices.java to understand the tool execution flow and the immediate return logic:
Assistant
Let me continue reading the rest of the file, especially looking for the part that handles immediate returns.
Assistant
Let me continue reading, especially the remaining part which likely contains the execution
Assistant
This file is mostly builder methods. The actual execution logic must be in DefaultAiServices. Let me find it.
Assistant
Let me read DefaultAiServices.java to understand the execution logic.
Assistant
Let me search for IMMEDIATE and Result references in DefaultAiServices first
Assistant
Let me look at ToolServiceResult and the ToolService more carefully
Assistant
Let me read the relevant sections of DefaultAiServices.java
Assistant
Let me look at the part of ToolService where executeInferenceAndToolsLoop is defined, focusing on how it uses isReturnTypeResult
Assistant
Let me read the executeInferenceAndToolsLoop method in ToolService.java
Assistant
Let me look at the shouldReturnImmediately method and the ToolExecution class
Assistant
Let me read the shouldReturnImmediately method and the rest
Assistant
Let me look at ToolExecution and the related classes
Assistant
Let me look at ToolExecutionResult
Assistant
Let me look at the existing tests related to this
Assistant
Let me check ToolServiceContext
Assistant
Let me look at the AbstractAiServiceWithToolsIT test to understand how tests work
Assistant
Let me read the test that throws an error when immediate tool is used without Result return
Assistant
Let me look for the StringAssistant interface and the test infrastructure
Assistant
Let me see the StringAssistant interface:
Assistant
Let me check the utility methods for type checking
Assistant
Let me look at TypeUtils.java
Assistant
Let me look at the existing test that verifies the exception is thrown
Assistant
Let me now look at the full DefaultAiServices.java to understand where to make changes
Assistant
Now I have a clear understanding. Let me implement the changes.
First, let me modify
ToolService.javato remove the exception throw:Assistant
Let me try to open the file differently.
Assistant
Let me use edit directly to make the changes.
Assistant
Let me use bash commands directly for the edits.
Assistant
Now let me edit the ToolService.java to remove the exception throw:
Assistant
Let me use sed to make the changes
Assistant
Now let me look at the exact lines in DefaultAiServices.java that need to be modified
Assistant
Let me check the imports in DefaultAiServices
Assistant
Let me see more of the imports
Assistant
Let me check what's already imported
Assistant
I need to import
ReturnBehavior,ToolExecution, andIllegalConfigurationExceptionclass (not just the static method). Let me check what other imports are needed.Actually,
IllegalConfigurationException.illegalConfigurationis statically imported. I need to also import the class itself to use it in the method signature or to throw it directly (though the static method should be fine).Let me check:
illegalConfigurationis a static method that creates and throws anIllegalConfigurationException. So I can use it directly.But I also need:
dev.langchain4j.agent.tool.ReturnBehavior- for checking TO_LLMdev.langchain4j.service.tool.ToolExecution- for getting result objects
Let me check if
ToolExecutionis already imported.Assistant
Let me add the necessary imports first. Let me check exactly where to add them:
Assistant
Let me add the imports. I'll add
ToolExecutionnear the existingToolServiceContext/ToolServiceResultimports, andReturnBehaviornear other dev.langchain4j importsAssistant
Let me verify the import was
Assistant
Now add the ReturnBehavior import
Assistant
Add ReturnBehavior import after dev.langchain4j.Internal
Assistant
Now let me make the main code changes. First, let me modify the immediate return handling block in DefaultAiServices.java. I need to add an
ifblock for non-Result immediate returns after the existingifblock.Let me look at the exact lines again:
Assistant
Let me see the exact line numbers
Assistant
Let me see lines 363-366:
Assistant
Let me insert the new code block after line 363
Assistant
Let me verify the changes
Assistant
Now I need to add the
handleImmediateToolReturnmethod. Let me find a good placeAssistant
Let me look at the class structure around the handler
Assistant
Let me find the structure of the class to find where to add the new method
Assistant
Let me see the area around fireEventAndReturn to understand the anonymous class structure
Assistant
Now let me add the
handleImmediateToolReturnmethod afterfireEventAndReturn. Let me insert after line 413:Assistant
Let me verify the changes were made correctly:
Assistant
Let me check the formatting around line 465:
Assistant
Let me check the full area:
Assistant
Let me add a blank line between the methods
Assistant
Let me verify the fix:
Assistant
Let me fix the formatting more properly:
Assistant
Let me verify again:
Assistant
Let me now compile the code to check for any issues
Assistant
Let me try setting HOME:
Assistant
Compilation succeeded. Now let me compile the tests too
Assistant
Compilation succeeded. Now let me check the existing test that validates throwing behavior when using immediate tools without Result return type.
Assistant
Let me find the ImmediateToolWithPrimitiveParameters
Assistant
Let me look at the tool classes
Assistant
Let me check how primitive types are handled in the codebase
Assistant
Let me update the
handleImmediateToolReturnmethod to handle type conversion better, especially for String return types and primitive typesAssistant
Now let me find the exact lines of the current method and
Assistant
Let me check lines 468-472:
Assistant
Let me delete the old method (lines 415-470) and insert the new one
Assistant
Now let me insert the new method at line 415:
Assistant
Let me verify the result
Assistant
Let me verify the full method and surrounding code
Assistant
Let me verify the isImage method follows
Assistant
Let me compile again to verify
Assistant
Good, compilation succeeds. Let me also compile tests
Assistant
Now let me run the specific test to see if it passes
Assistant
Let me run the ReturnBehaviorCombinationsTest
Assistant
Let me run a few more tests related to the immediate return feature
Assistant
Let me run the AbstractAiServiceWithToolsIT test (which contains the
should_throw_using_immediate_tool_on_service_not_returning_Resulttest)Assistant
Let me run the specific test more explicitly
Assistant
Let me find the actual test class that implements this
Assistant
Let me find the concrete test classes
Assistant
Let me reconsider - the
toString()fallback might be too lenient. Let me remove it and keep only direct type matchingAssistant
Let me replace the method with the updated version
Assistant
Let me delete and replace both methods
Assistant
Now insert the new method at line 416:
Assistant
Let me verify the result
Assistant
Let me fix the formatting - add a blank line before
isImage:Assistant
Let me verify the formatting
Assistant
Let me check the full area around isImage
Assistant
Let me add a blank line before the isImage method:
Assistant
Let me verify
Assistant
Good. Let me compile again to verify everything works
Assistant
Let me verify the overall structure of the file is correct - let me check the class boundaries
Assistant
Let me check what happens when a tool method returns void
Assistant
Let me check how void tool methods are handled
Assistant
Let me check if
isReturnTypeResultis still used anywhere in the codebase that I need to worry aboutAssistant
Let me run some of the relevant tests to confirm nothing is broken
Assistant
All tests pass. Let me run a broader set of tests in the langchain4j module
Assistant
Let me check if there were test failures
Assistant
All 1385 tests pass! Let me do a final review of the changes I've made.
Assistant
Let me clean up the extra blank lines in the diff:
Assistant
Let me fix the extra blank lines
Assistant
Let me verify the fix:
Assistant
Let me recompile to verify
Assistant
The
ReturnBehaviorenum's Javadoc is now outdated. Let me update it to reflect the new behavior.Assistant
Let me update the Javadoc to reflect the new behavior
Assistant
I should update this Javadoc. Let me replace the text.
Assistant
Let me verify the updated Javadoc
Assistant
I need to add the
<p>tag back before the new textAssistant
Let me verify the javadoc again
Assistant
Let me check the Javadoc for the IMMEDIATE and IMMEDIATE_IF_LAST constants
Assistant
Let me update the enum constant docs too
Assistant
Let me verify the changes
Assistant
Let me fix the indentation on the new Javadoc lines
Assistant
Let me verify the full Javadoc
Assistant
Let me compile to ensure everything is OK
Assistant
Let me run the key tests one more time to confirm everything passes
Assistant
Let me check the final git diff
Assistant
All changes look good. Let me now submit.
Checklist:
-
If an error was reported in the issue description, I have successfully reproduced it. - This is a feature request, not a bug report. The issue is that non-Result return types are not supported for AiService methods when tool execution uses IMMEDIATE return behavior.
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error. - N/A (feature request). My implementation adds support for non-Result return types with the rules specified in the issue.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.