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 exploring the repository structure to understand what we're working with.
Assistant
This is a langchain4j repository. The issue is about supporting non-Result return types for AiService when tool execution is IMMEDIATE. Let me first search for relevant code related to AiService and ReturnBehavior.
<tool_call> <function=bash_terminal> <parameter=command> find . -type f -name "*.java" | xargs grep -l "ReturnBehavior" 2>/dev/null | head -20 </tool_call>
Assistant
Let me search for ReturnBehavior in the codebase.
Assistant
Let me look at the ReturnBehavior enum to understand the different values available.
<tool_call> <function=bash_terminal> <parameter=command> find . -type f -name "ReturnBehavior.java" 2>/dev/null </tool_call>
Assistant
Let me search for ReturnBehavior.java file.
Assistant
Let me read the ReturnBehavior enum.
<tool_call> <function=bash_terminal> <parameter=command> cat ./langchain4j-core/src/main/java/dev/langchain4j/agent/tool/ReturnBehavior.java </tool_call>
Assistant
I need to actually invoke the bash_terminal function with a command. Let me read the ReturnBehavior.java file.
Assistant
Now let me look at the ToolService.java file, which seems to be the main class handling tool execution and ReturnBehavior logic.
<tool_call> <function=bash_terminal> <parameter=command> cat ./langchain4j/src/main/java/dev/langchain4j/service/tool/ToolService.java </tool_call>
Assistant
Let me use the proper bash_terminal function to read the ToolService.java file.
Assistant
Now let me look at the specific part of ToolService.java that handles the immediate return behavior. I'll need to search for where the exception is thrown about AiService method return types.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "AI Service method must return" ./langchain4j/src/main/java/dev/langchain4j/service/tool/ToolService.java </tool_call>
Assistant
I need to use bash_terminal with a command. Let me search for the specific error message.
Assistant
Now let me view the lines around line 475 to understand the context better.
Assistant
Let me use a different approach to see the file content around line 475.
Assistant
Let me also see the
shouldReturnImmediatelymethod to understand the logic better.<tool_call> <function=bash_terminal> <parameter=command> grep -n "shouldReturnImmediately" ./langchain4j/src/main/java/dev/langchain4j/service/tool/ToolService.java </tool_call>
Assistant
The issue is that I need to invoke bash_terminal correctly. Let me try again.
Assistant
Let me view the shouldReturnImmediately method implementation.
Assistant
Now I need to understand what happens after
shouldReturnImmediatelyreturns true. The code currently throws an exception if the return type is notResult. Let me look for where theToolServiceResultis converted to the actual return value.Assistant
Let me look at the DefaultAiServices.java file to see how ToolServiceResult is used and converted to the final result.
Assistant
I need to properly invoke bash_terminal. Let me try again.
Assistant
Now I understand the code flow. Let me look at the
immediateToolReturn()case more carefully. WhenimmediateToolReturn()is true and the return type isResult, it returns aResultobject. But the issue is that when the return type is notResult, it currently throws an exception.Let me see the ToolServiceResult class to understand what data is available when
immediateToolReturn()is true.Assistant
Now let me look at the ToolExecution class to understand what results are available from tool executions.
<tool_call> <function=bash_terminal> <parameter=command> cat ./langchain4j/src/main/java/dev/langchain4j/service/tool/ToolExecution.java </tool_call>
Assistant
I need to invoke bash_terminal properly to see the ToolExecution class.
Assistant
Now let me look at the ToolExecutionResult class to understand the result structure.
Assistant
Now let me look at the DefaultAiServices.java file to see the full context of where the exception is thrown.
<tool_call> <function=bash_terminal> <parameter=command> head -n 400 ./langchain4j/src/main/java/dev/langchain4j/service/DefaultAiServices.java | tail -n 100 </tool_call>
Assistant
I need to use the bash_terminal function with the proper command to get lines 330-400 from DefaultAiServices.java.
Assistant
Now let me look at the ToolService.executeInferenceAndToolsLoop to see where the exception is thrown. I need to find the full method.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '406,520p' ./langchain4j/src/main/java/dev/langchain4j/service/tool/ToolService.java </tool_call>
Assistant
I need to use bash_terminal to execute the command properly. Let me try again.
Assistant
Now I have a clear understanding of the issue. Let me summarize:
-
The
ToolService.executeInferenceAndToolsLoopmethod is responsible for handling tool execution and determining when to return immediately based onReturnBehavior. -
When
shouldReturnImmediatelyreturns true, the code currently checks if the return type isResult. If not, it throws anIllegalConfigurationException. -
According to the issue, we need to support non-
Resultreturn types when all tool executions are IMMEDIATE:- 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
-
Assistant
I need to properly invoke bash_terminal to search for test files with ReturnBehavior.
Assistant
Let me look at the ProgrammaticCreatedImmediateReturnToolTest to understand the current behavior.
Assistant
Now let me look at the ReturnBehaviorCombinationsTest to see what tests exist for the immediate return behavior.
Assistant
I need to understand the full structure of DefaultAiServices.java to properly implement the fix. Let me view more of the file.
<tool_call> <function=bash_terminal> <parameter=command> wc -l ./langchain4j/src/main/java/dev/langchain4j/service/DefaultAiServices.java </tool_call>
Assistant
I need to properly invoke bash_terminal to check the line count of DefaultAiServices.java.
Assistant
The file has 828 lines. Let me look at the section around lines 330-400 where the immediateToolReturn is handled, and understand the full context.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '350:420p' ./langchain4j/src/main/java/dev/langchain4j/service/DefaultAiServices.java </tool_call>
Assistant
The command is not being executed properly. Let me try a different approach to see the file content.
Assistant
Now I need to understand the issue better. The code currently:
- When
toolServiceResult.immediateToolReturn()is true andisReturnTypeResultis true, it returns aResultobject with null content.
- When
Assistant
I need to use bash_terminal to run the command properly. Let me try again.
Assistant
Now I need to look at the structure of the file to understand where the exception is thrown and where the fix should be applied. Let me look at the ToolService.executeInferenceAndToolsLoop method more closely.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '406:520p' ./langchain4j/src/main/java/dev/langchain4j/service/tool/ToolService.java </tool_call>
Assistant
I need to use bash_terminal properly to run commands. Let me try again with the correct format.
Assistant
Now I understand the issue. The code in
ToolService.executeInferenceAndToolsLoopthrows anIllegalConfigurationExceptionwhenshouldReturnImmediatelyis true butisReturnTypeResultis false.According to the issue, we need to support non-Result return types when tool execution is IMMEDIATE. The rules are:
- 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
The fix should be:
- Remove the exception throwing in
ToolService.executeInferenceAndToolsLoop - Modify
ToolServiceResultto include the tool execution results - Modify
DefaultAiServicesto handle the immediate return case for non-Result return types
Let me first check what
TypeUtilsprovides for checking if a type is void or nullable.<tool_call> <function=bash_terminal> <parameter=command> cat ./langchain4j/src/main/java/dev/langchain4j/service/TypeUtils.java </tool_call>
Assistant
I need to use bash_terminal properly to read the TypeUtils.java file.
Assistant
Now I have a better understanding of the codebase. Let me plan my changes:
-
In
ToolService.executeInferenceAndToolsLoop:- Remove the exception when
isReturnTypeResultis false andshouldReturnImmediatelyis true - Instead, always return a
ToolServiceResultwithimmediateToolReturn=true
- Remove the exception when
-
In
ToolServiceResult:- The class already has
toolExecutions()which contains the tool execution results - We don't need to add any new fields
- The class already has
-
In
DefaultAiServices:- Modify the handling of
toolServiceResult.immediateToolReturn()to handle non-Result return types - Apply the rules from the issue:
- If return type is void, return null
- If any tool executions are TO_LLM (already handled by shouldReturnImmediately returning false), throw exception
- If all tool execution results are null and return type is nullable, return null
- Modify the handling of
-
Assistant
I need to read more of the DefaultAiServices.java file to understand how the return type is handled and where the logic for immediateToolReturn is.
Assistant
Let me continue looking at the code to understand where the returnType is used.
Assistant
Let me try a different approach to read the file.
Assistant
Let me check where
returnTypeis defined.Assistant
Let me look at line 239 to see where
returnTypeis defined.Assistant
Now I have a good understanding of the code structure. Let me now implement the fix.
The fix involves:
-
In
ToolService.executeInferenceAndToolsLoop: Remove the check that throws an exception whenisReturnTypeResultis false. Instead, always return aToolServiceResultwithimmediateToolReturn=true. -
In
DefaultAiServices: Handle the immediate return case for non-Result return types. WhentoolServiceResult.immediateToolReturn()is true andisReturnTypeResultis false, apply the following rules:- If return type is void (or Void), return null
- If all tool execution results are null and return type is nullable, return null
- If there's one non-null tool execution result that can be resolved to the return type, return that object
Let me now look at the exact lines in ToolService.java where the exception is thrown.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "isReturnTypeResult" ./langchain4j/src/main/java/dev/langchain4j/service/tool/ToolService.java </tool_call>
-
Assistant
I need to use bash_terminal to execute grep command properly.
Assistant
Now let me look at the full context around line 473 to understand the exact code to modify.
Assistant
Now I understand the code. Let me implement the fix:
- In
ToolService.java, remove the exception throwing and always returnToolServiceResultwithimmediateToolReturn=true. - In
DefaultAiServices.java, handle the immediate return case for non-Result return types.
Let me start by modifying
ToolService.java:- In
Assistant
The sed command is not working correctly. Let me use a different approach to read the exact lines.
Assistant
Now I need to use the
editcommand to make the changes. Let me first modify theToolService.javafile to remove the exception throwing and always return theToolServiceResult.Assistant
The edit command made a mistake - it duplicated some lines. Let me fix this by reverting and trying again.
Assistant
Let me first read the exact lines that need to be modified. I'll use a more careful approach with the edit command.
Assistant
Let me view more context to get the exact lines to replace:
Assistant
Let me use the replace command instead to make the change more precisely:
Assistant
The change to
ToolService.javais complete. Now I need to modifyDefaultAiServices.javato handle the immediate return case for non-Result return types.Let me first look at the current code in
DefaultAiServices.javathat handles the immediateToolReturn case:<tool_call> <function=bash_terminal> <parameter=command> head -n 400 ./langchain4j/src/main/java/dev/langchain4j/service/DefaultAiServices.java | tail -n 70 </tool_call>
Assistant
I need to read more of the DefaultAiServices.java file to understand the current implementation.
Assistant
Assistant
The replace command was not invoked. I need to use the bash_terminal function to execute the command properly. Let me try again with the correct syntax.
Assistant
The search string was found at line 349. Let me navigate there first and then make the replacement.
Assistant
Now I'm at line 349. Let me use the edit command to replace the specific lines (349-361) and add the new logic.
Assistant
Now I need to add the
resolveImmediateToolReturnResultmethod. Let me add it after thefireEventAndReturnmethod.Assistant
I see there's a duplicate line. Let me fix that:
Assistant
The edit still left a duplicate line. Let me fix this properly.
Assistant
The edit still left a duplicate line. Let me fix this by replacing the duplicate lines.
<tool_call> <function=bash_terminal> <parameter=command> edit 487:488 << 'EOF' if (isImageType(rawReturnType)) { EOF </tool_call>
Assistant
The edit command still needs to be invoked properly. Let me try again with the correct syntax.
Assistant
The duplicate line is still there. Let me try a different approach - I'll revert the file and make the changes more carefully.
Assistant
Let me start fresh and make the changes more carefully. First, let me view the exact lines to modify:
Assistant
Let me read more context around the lines I need to modify:
Assistant
Now I understand the structure better. Let me make the changes step by step:
- First, modify the condition that checks for immediate tool return (around lines 349-361)
- Then, add the
resolveImmediateToolReturnResultmethod afterfireEventAndReturn(after line 413)
Let me first get the exact line numbers:
Assistant
Let me get the exact lines that need to be modified:
Assistant
Now let me modify lines 349-361 to change the condition and add the new logic:
Assistant
Now let me verify the change was applied correctly:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '349,370p' ./langchain4j/src/main/java/dev/langchain4j/service/DefaultAiServices.java </tool_call>
Assistant
I need to use bash_terminal to run the command to check the file.
Assistant
The modification was applied correctly. Now I need to add the
resolveImmediateToolReturnResultmethod. Let me find the line number wherefireEventAndReturnends:<tool_call> <function=bash_terminal> <parameter=command> grep -n "private Object fireEventAndReturn" ./langchain4j/src/main/java/dev/langchain4j/service/DefaultAiServices.java </tool_call>
Assistant
I need to use bash_terminal to run the grep command to find the line number.
Assistant
The line 407 is where fireEventAndReturn starts. Let me check where it ends (around line 414):
<tool_call> <function=bash_terminal> <parameter=command> sed -n '407,420p' ./langchain4j/src/main/java/dev/langchain4j/service/DefaultAiServices.java </tool_call>
Assistant
I need to use bash_terminal to run the sed command to check the lines.
Assistant
The fireEventAndReturn method ends at line 413 (the closing brace). Now I need to add the resolveImmediateToolReturnResult method after line 413 and before line 415 (the isImage method).
Let me insert the new method at line 414:
Assistant
Let me verify that the changes were applied correctly:
Assistant
The changes have been applied. Now let me try to compile the code to make sure there are no errors.
Assistant
I need to add the import for ToolExecution in DefaultAiServices.java. Let me check the current imports:
Assistant
Let me continue checking the imports:
Assistant
I need to add the import for ToolExecution. Let me add it after the ToolServiceResult import:
Assistant
Let me verify that the import was added correctly and then try to compile again:
Assistant
Let me use a different approach to check the imports:
Assistant
Good, the import was added correctly. Now let me try to compile again:
Assistant
The compilation succeeded. Now let me run the existing tests to make sure the changes don't break anything:
Assistant
The tests passed (no error output). Let me also run the ProgrammaticCreatedImmediateReturnToolTest to make sure the changes work correctly:
Assistant
Good, the tests passed. Let me also run the StreamingProgrammaticCreatedImmediateReturnToolTest to make sure streaming functionality still works:
Assistant
Assistant
I need to invoke bash_terminal to create the test file.
Assistant
Assistant
I need to invoke bash_terminal to run a command. Let me try a simpler approach by using mvn to compile and run the test from the test directory.
Assistant
I need to properly invoke bash_terminal to run a command. Let me write the test file to the test directory and run it using mvn.
<tool_call> <function=bash_terminal> <parameter=command> cat > /tmp/test_nonresult_return.sh << 'TESTSCRIPT' #!/bin/bash
Copy the test file to the test directory
cp /tmp/TestNonResultReturn.java /langchain4j/langchain4j/src/test/java/dev/langchain4j/service/tool/
Run the test
cd /langchain4j mvn test -pl langchain4j -Dtest=TestNonResultReturn 2>&1 TESTSCRIPT
chmod +x /tmp/test_nonresult_return.sh /tmp/test_nonresult_return.sh </tool_call>
Assistant
The command is not being executed because I need to invoke bash_terminal correctly. Let me try again with the proper syntax.
Assistant
I need to run the test file that I created. Let me use mvn to run it.
Assistant
Assistant
I need to use bash_terminal properly to run the command. Let me try again.
Assistant
I need to update the
resolveImmediateToolReturnResultmethod to handle the case where the return type is String and the tool result is not null but doesn't match the return type. In this case, I should convert the result to a String using String.valueOf().Assistant
I need to update the code to handle the case where the return type is String and the tool result is not null but doesn't match the return type. In this case, I should convert the result to a String using String.valueOf().
<tool_call> <function=bash_terminal> <parameter=command> sed -i '461a
// If return type is String, convert result to String
if (rawReturnType == String.class && result != null) {
return String.valueOf(result);
}' /langchain4j/langchain4j/src/main/java/dev/langchain4j/service/DefaultAiServices.java </tool_call>Assistant
I need to use bash_terminal properly to run the sed command. Let me try again with the correct syntax.
Assistant
Let me verify the change was applied correctly:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '455,475p' /langchain4j/langchain4j/src/main/java/dev/langchain4j/service/DefaultAiServices.java </tool_call>
Assistant
I need to use bash_terminal to run the sed command to check the file.
Assistant
The change was applied correctly. Now let me run the test again to see if it passes:
<tool_call> <function=bash_terminal> <parameter=command> cd /langchain4j && mvn test -pl langchain4j -Dtest=TestNonResultReturn -q 2>&1 </tool_call>
Assistant
I need to use bash_terminal properly to run the test command. Let me try again.
Assistant
All tests passed. Now let me remove the test file I created and run the existing tests to make sure the changes don't break anything.
Assistant
Now let me run the existing tests to make sure the changes don't break anything.
Assistant
The ReturnBehaviorCombinationsTest passed. Let me also run the ProgrammaticCreatedImmediateReturnToolTest:
Assistant
The ProgrammaticCreatedImmediateReturnToolTest passed. Let me also run the StreamingProgrammaticCreatedImmediateReturnToolTest:
Assistant
The StreamingProgrammaticCreatedImmediateReturnToolTest passed. Let me also run the LazyCalculationIntegrationTest:
<tool_call> <function=bash_terminal> <parameter=command> cd /langchain4j && mvn test -pl langchain4j -Dtest=LazyCalculationIntegrationTest -q 2>&1 </tool_call>
Assistant
Let me run the LazyCalculationIntegrationTest with the correct command.
Assistant
Great, all the ReturnBehavior-related tests passed. Now let me run a broader set of tests related to AiServices to make sure the changes don't break anything else.
<tool_call> <function=bash_terminal> <parameter=command> cd /langchain4j && mvn test -pl langchain4j -Dtest=AiServicesWithTools* -q 2>&1 </tool_call>
Assistant
Let me run the AiServicesWithTools tests.
Assistant
The tests passed. Let me also run a few more related tests to make sure the changes are correct.
<tool_call> <function=bash_terminal> <parameter=command> cd /langchain4j && mvn test -pl langchain4j -Dtest=AiServicesToolName* -q 2>&1 </tool_call>
Assistant
Let me run the test properly.
Assistant
Let me verify the changes I made by viewing the git diff:
Assistant
Let me run a broader set of tests to ensure the changes don't break anything:
Assistant
The changes are complete. Let me provide a summary of the changes made:
Summary of Changes
1. ToolService.java
Removed the exception that was thrown when
ReturnBehavior.IMMEDIATEorReturnBehavior.IMMEDIATE_IF_LASTwas used with non-Result return types. Now the code always returns aToolServiceResultwithimmediateToolReturn=truewhen the immediate return condition is met.2. DefaultAiServices.java
Added support for non-Result return types when tool execution is IMMEDIATE:
- Added import for
ToolExecutionclass - Modified the immediate tool return handling to also handle non-Result return types
- Added
resolveImmediateToolReturnResultmethod that implements the rules specified in the issue:
- Added import for
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.