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
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.
Assistant
Let me start by understanding the repository structure and the issue at hand. The issue is about supporting non-Result return types for AiService when tool execution is IMMEDIATE. Let me explore the repository to understand its structure and find the relevant code.
Assistant
Let me explore the repository structure more specifically looking for AiService-related files.
Assistant
Let me explore the relevant source files:
Assistant
Let me continue exploring the codebase to understand how the Result type is currently handled and where the changes need to be made.
Assistant
Let me explore the repository structure more systematically, looking at the relevant files for this feature. The issue is about supporting non-Result return types for AiService when tool execution is IMMEDIATE. I need to understand:
- The ReturnBehavior enum
- How AiServices currently handles tool execution results
- Where the Result wrapper is used
- The Quarkus PR mentioned in the issue for reference
Let me start by reading the key files to understand the current implementation.
Assistant
Let me continue exploring the relevant files. I need to understand how the Result type is used in the service output handler and where the changes need to be made.
Assistant
Now let me look at the AiServiceTokenStreamHandler and DefaultAiServices classes to understand how results are returned:
<tool_call> <function=bash_terminal> <parameter=command> find /langchain4j -type f -name "DefaultAiServices.java" -o -name "AiServiceTokenStreamHandler.java" | xargs cat 2>/dev/null </tool_call>
Assistant
Let me search for these files separately.
Assistant
Found it. Let me read the DefaultAiServices.java file.
Assistant
The file is long. Let me focus on the specific part that handles tool execution and the Result type:
Assistant
Let me see the full relevant section of DefaultAiServices.java:
Assistant
Let me see the ToolServiceResult and ToolServiceContext classes:
Assistant
Now let me look at the ToolServiceContext to understand how tool execution results are tracked:
Assistant
Now let me look at the executeInferenceAndToolsLoop method in ToolService:
Assistant
Let me look at the full executeInferenceAndToolsLoop method in ToolService.java to understand the current implementation.
Assistant
Let me look at the existing test files and the Quarkus PR to better understand the expected behavior:
Assistant
Let me examine the test files to understand how the current IMMEDIATE behavior works.
Assistant
Let me continue exploring the test files to understand the current behavior and what changes are needed.
Assistant
Let me look at the ToolExecution class and how tool results are tracked:
Assistant
Now let me look at the ToolExecutionResult class to understand how the tool result object is stored:
Assistant
Now I have a good understanding of the codebase. Let me look at the existing tests to understand the expected behavior better, and then implement the feature.
Assistant
Let me explore the test file more to understand the current behavior better.
Assistant
Let me look at the current test for throwing an error when using immediate tool on a service not returning Result:
Assistant
Now let me look at the ToolExecutionResult more closely to understand how result objects are stored and accessed:
Assistant
Now I have a good understanding of the codebase. Let me summarize my findings and plan the implementation:
Current Behavior:
- When a tool uses
ReturnBehavior.IMMEDIATEorReturnBehavior.IMMEDIATE_IF_LAST, the AiService must returnResult<T>, otherwise anIllegalConfigurationExceptionis thrown inToolService.executeInferenceAndToolsLoop()
New Behavior Required:
- If AiService return type is
void, returnnull - If any tool execution has
TO_LLMbehavior, throw exception (for theIMMEDIATE_IF_LASTcase where it's last) - If all tool results are
nulland return type is nullable, returnnull - If there's one non-null tool result that can be resolved to the return type, return that object
Let me implement the changes:
- When a tool uses
Assistant
Now I have a comprehensive understanding of the codebase. Let me implement the feature. The changes need to be made in two files:
ToolService.java- Remove the exception when!isReturnTypeResultand allow the immediate returnDefaultAiServices.java- Handle the non-Result return type case whenimmediateToolReturnis true
Let me start by examining the exact code sections I need to modify:
Assistant
Now I have a comprehensive understanding of the codebase. Let me implement the feature step by step.
Plan:
- In
ToolService.java, remove the exception when!isReturnTypeResultfor immediate returns - the tool service should just return theToolServiceResultwithimmediateToolReturn=true - In
DefaultAiServices.java, handle the case whenimmediateToolReturnis true and!isReturnTypeResultby determining the appropriate value to return based on the rules in the issue
Let me start with the first change:
- In
Assistant
Let me use the edit command properly with the correct syntax.
Assistant
I need to use the edit command with proper syntax. Let me look at the format again.
Assistant
Let me use bash to perform the edit instead:
Assistant
The user wants me to fix the edit command syntax. Let me try using bash with sed to make the changes instead.
Assistant
Let me use sed to make the edit:
Assistant
Let me verify the change was made correctly:
Assistant
Now I need to modify the DefaultAiServices.java to handle non-Result return types when
immediateToolReturnis true. Let me look at the relevant section:Assistant
Now I need to modify the ToolServiceResult class to track whether any tool has TO_LLM behavior, and then modify DefaultAiServices to handle non-Result return types.
Assistant
Now let me continue with the implementation. I need to add a field to ToolServiceResult to track if any tool has TO_LLM behavior (for the IMMEDIATE_IF_LAST case), and then implement the non-Result return logic in DefaultAiServices.
First, let me check the current state of the ToolServiceResult:
Assistant
Now I need to modify the DefaultAiServices.java to handle non-Result return types when
immediateToolReturnis true. Let me look at the relevant section:Assistant
Now I understand the full picture. I need to:
- Modify
ToolServiceResultto track whether any tool hasTO_LLMbehavior (needed forIMMEDIATE_IF_LASTcase) - Modify
DefaultAiServicesto handle non-Result return types whenimmediateToolReturnis true
Let me first modify
ToolServiceResultto add a field to track if any tool hasTO_LLMbehavior:- Modify
Assistant
Let me check the current state of the ToolService.java file to verify the previous edit:
Assistant
Good, the edit to ToolService.java was successful. Now I need to modify DefaultAiServices.java to handle non-Result return types when immediateToolReturn is true. Let me look at the relevant section again:
Assistant
Now I need to modify DefaultAiServices.java to handle non-Result return types when
immediateToolReturnis true. Based on the issue description, I need to:- If ai service method return type is void, just return null
- 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
Let me create the updated version:
Assistant
Let me understand the issue better by looking at what the current behavior is and what needs to change. I need to modify the handling in
DefaultAiServices.javafor the case whenimmediateToolReturnis true butisReturnTypeResultis false.Assistant
Let me continue working on the implementation. I need to modify DefaultAiServices.java to handle the case when
immediateToolReturnis true butisReturnTypeResultis false.Assistant
Now let me continue with the implementation. I need to modify
DefaultAiServices.javato handle the case whenimmediateToolReturnis true butisReturnTypeResultis false.Assistant
I need to modify
DefaultAiServices.javato handle the case whenimmediateToolReturnis true butisReturnTypeResultis false. Let me add logic to resolve the return value based on tool execution results.Assistant
Let me continue with the implementation. I need to modify DefaultAiServices.java to add logic for handling non-Result return types when
immediateToolReturnis true.Assistant
I need to add logic to handle non-Result return types when
immediateToolReturnis true. Let me do this by adding an else block after the existing Result handling.Assistant
Let me continue with the implementation. I need to:
- Modify the condition in DefaultAiServices.java to add an else block for non-Result return types
- Add a new method
resolveImmediateReturnValuethat handles the logic described in the issue
Let me first modify the existing condition and then add the new method.
Assistant
Let me modify the DefaultAiServices.java to add the handling for non-Result return types when immediateToolReturn is true.
Assistant
Now I need to replace the existing section. Let me use a different approach - I'll create a complete replacement using sed.
Assistant
The user wants me to continue with the implementation. I need to:
- Modify the condition in DefaultAiServices.java to add an else block for non-Result return types
- Add a new method
resolveImmediateReturnValuethat handles the logic described in the issue
Let me continue with the implementation.
Assistant
Let me verify the change was made correctly.
Assistant
I see there's a problem - there's an extra closing brace. Let me fix this:
Assistant
I see the issue - there's an extra closing brace and the original if block's closing brace is missing. Let me fix this:
Assistant
I see the issue - the file has structural problems. Let me fix it properly.
Assistant
I need to fix the file structure. The issue is:
- The original
ifblock is missing its closing brace - There's an extra closing brace after the new code
Let me fix this by:
- Adding a closing brace after
return fireEventAndReturn(invocationContext, result); - Removing the extra closing brace after the new if block
Looking at the current state:
- Line 361:
return fireEventAndReturn(invocationContext, result); - Line 362: blank
- Line 363:
if (toolServiceResult.immediateToolReturn() && !isReturnTypeResult) { - Line 373:
}(closing brace of new if block) - Line 374:
}(extra closing brace that needs to be removed)
I need to:
- Add a
}after line 361 to close the original if block - Remove the extra
}on line 374
- The original
Assistant
I need to fix the file structure. Let me add the missing closing brace and remove the extra one.
Assistant
Let me fix the file structure properly by removing the extra closing brace and adding the missing one:
Assistant
It seems there's still an extra closing brace. Let me look at the file more carefully and fix it properly.
Assistant
Let me check the exact line numbers and fix the issue properly:
Assistant
Let me fix the file properly by identifying the exact line numbers:
Assistant
I need to fix the file structure. Let me understand the current state:
- Line 360:
return fireEventAndReturn(invocationContext, result); - Line 361: (blank)
- Lines 362-370: The new if block I added
- Line 371:
ChatResponse aggregateResponse = ...
Wait, looking at the output again:
- Line 368:
}(closing brace of new if) - Line 369:
}(extra closing brace) - Line 370: (blank)
- Line 371:
ChatResponse aggregateResponse = ...
But wait, looking at the sed output, it shows:
- Line 11 (368):
}(closing brace of new if) - Line 12 (369):
}(extra closing brace)
- Line 360:
Assistant
I need to fix the file structure. The original if block is missing its closing brace, and there's an extra closing brace.
Assistant
Let me understand the current state of the file and fix it properly.
Assistant
I see the issue now. The diff shows that my new code was inserted before the original closing brace of the first if block, which broke the structure. Let me fix this by restoring the original file and making the correct edit.
Assistant
Let me check the current state of the file after the git checkout:
Assistant
The file has been restored. Now I need to properly add the new code block. The key is to:
- Add a new
ifblock AFTER the existing closing brace (line 360) - Make sure the structure is correct
Let me use sed to insert the new code after line 360 (the closing brace of the existing if block).
- Add a new
Assistant
I see the file is still broken. Let me fix it properly by restoring and making the correct edit this time.
Assistant
The issue is that when I use sed to insert lines after line 360, I'm inserting before the closing brace of the original if block. Let me look at the file structure more carefully and understand what's happening.
Looking at the sed output:
code360: return fireEventAndReturn(invocationContext, result); 361: (blank) 362: } 363: (blank) 364: ChatResponse aggregateResponse = toolServiceResult.aggregateResponse();When I do
sed -i '360a\...', it inserts after line 360 but BEFORE line 361. So the new code ends up between the return statement and the closing brace.I need to insert AFTER line 362 (the closing brace of the original if block).
Assistant
Now I understand the structure. The closing brace is at line 361 (358+3=361). I need to insert the new code after line 361 (the closing brace).
Assistant
The file structure looks correct now. Now I need to add the
resolveImmediateReturnValuemethod to the DefaultAiServices class. Let me find a good place to add this method.Assistant
I need to add the
resolveImmediateReturnValuemethod to DefaultAiServices.java. Let me find a good place to add it.Assistant
Now I need to add the
resolveImmediateReturnValuemethod. Let me add it after thefireEventAndReturnmethod.Assistant
Now I need to add the
resolveImmediateReturnValuemethod. Let me add it after thefireEventAndReturnmethod.Assistant
I need to fix a bug in the code I just added - for
char.class, I'm returning 0 instead of'\0'. Let me fix that.Assistant
I see that the fix didn't work as expected because the single quote handling is tricky. Let me try a different approach.
Assistant
I need to fix the char case. The issue is that the sed command didn't work as expected because of the single quote handling. Let me use a Python script to make the replacement.
Assistant
I need to continue with the implementation. Let me fix the char case and then test the compilation.
Assistant
Let me continue with the implementation. I need to fix the char case using perl.
Assistant
The perl command didn't work. Let me try a different approach using sed with a line-specific replacement:
Assistant
I need to fix the char case using sed with line numbers.
Assistant
Good, the fix is in place. Now let me verify the changes compile correctly and run the tests.
Assistant
I need to add the import for
ToolExecution. Let me check the current imports and add the missing one.Assistant
I need to add the import for
ToolExecution. Let me check the current imports and add the missing one.Assistant
I need to add the import for
ToolExecution. Let me add it after the existing tool imports.Assistant
Good, the import was added. Let me verify the changes compile correctly now.
Assistant
The compilation succeeded. Now let me run the existing tests to make sure I haven't broken anything.
Assistant
Good, the tests passed. Let me run more tests related to tool execution and ReturnBehavior.
Assistant
Good, the tests passed. Let me run more tests to ensure the implementation is correct.
Assistant
Let me run more tests and then create a test to verify the new functionality.
Assistant
Let me create a test to verify the new functionality for non-Result return types with IMMEDIATE tools.
Assistant
Let me continue with the implementation. I need to copy the test file to the correct location and run it.
Assistant
The user wants me to continue with the implementation. I need to:
- Fix the test file to use the correct imports
- Run the tests to verify the new functionality
Assistant
Let me fix the test file by adding the missing imports:
Assistant
I need to add the missing import for ToolSpecification in the test file. Let me fix the test file.
Assistant
I need to continue with the implementation. The test file has been updated with the missing import. Now I need to run the test to verify the new functionality.
Assistant
I need to continue with the implementation. The test is failing because I need to understand how the ToolExecutor creates the ToolExecutionResult. Let me check the ToolExecutor interface:
Assistant
I need to continue with the implementation. The issue is that the ToolExecutor's
executemethod returns a String, but theToolExecutionResultdoesn't store the raw result object. Let me check how the ToolExecutionResult is created and how to properly store the result object.Assistant
Let me look at the DefaultToolExecutor to understand how the result object is stored.
Assistant
I need to continue with the implementation. The issue is that the test is using a lambda ToolExecutor that only implements the
executemethod, which returns a String but doesn't store the result object. I need to modify the test to properly create ToolExecutionResults with the result object stored.Assistant
Let me run the test again to see if the updated test file works:
Assistant
I need to continue with the implementation. The issue is that when the ToolExecutor returns null, the ToolExecutionResult builder is complaining about null text. Let me investigate the issue further.
Assistant
Let me look at the issue more carefully. The problem is that when the tool returns null, the ToolExecutionResult builder is not handling it correctly. Let me fix the test.
Assistant
Let me run the test again to see if the updated test file works now:
Assistant
The test is failing because when the tool returns null, the ToolExecutionResultMessage builder requires some text or contents. Let me update the test to handle null results properly:
Assistant
Let me run the test again to see if the updated test file works now:
Assistant
All tests passed. Let me run more comprehensive tests to ensure the implementation is correct:
Assistant
All tests passed. Let me run more comprehensive tests to ensure the implementation doesn't break existing functionality:
Assistant
Let me run the specific tests related to the changes I made:
Assistant
Let me verify the changes I made by reviewing the git diff:
Assistant
Let me continue by running a broader set of tests to ensure the implementation doesn't break existing functionality:
Assistant
All tests are passing. Let me verify the implementation is complete by checking the git status and ensuring the test file is properly added:
Assistant
Let me run a more comprehensive set of tests to ensure the implementation is correct and doesn't break anything:
Assistant
All tests are passing. Let me now review the implementation to make sure it handles all edge cases correctly. Let me also remove the test file I created since it was just for verification:
Assistant
The user wants me to submit my changes since I've completed the implementation. Let me verify the final state and submit.
Assistant
The user is asking me to submit my changes since I've completed the implementation. Let me verify the final state and submit.
Assistant
The user is asking me to submit my changes. I need to use the submit command correctly. Let me check the format required.
Assistant
The user wants me to submit my changes. Let me check the format required for the submit command.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.