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
I'll start by exploring the repository structure to understand the codebase and locate relevant files for this feature. Let me first look at the overall structure.
- Assistant
Let me search for the core AiService and related files.
- Assistant
Let me search for Result class and ReturnBehavior enum.
- Assistant
Let me look at the key files:
langchain4j/src/main/java/dev/langchain4j/service/Result.java- the Result classlangchain4j-core/src/main/java/dev/langchain4j/agent/tool/ReturnBehavior.java- ReturnBehavior enum3.langchain4j/src/main/java/dev/langchain4j/service/DefaultAiServices.java- the main AiServices implementation
- Assistant
Let me explore the Result class, DefaultAiServices, and other relevant files to understand the current implementation.
- Assistant
Now let me look at the DefaultAiServices to understand how tool execution results are currently handled.
- Assistant
The file is truncated. Let me search for the immediate return handling logic in DefaultAiServices.
- Assistant
Now let me look at the specific section around line 336-395 to understand the current logic for handling return types.
- Assistant
Let me look at the ToolServiceResult and the tool service execution logic.
- Assistant
Let me find the executeInferenceAndToolsLoop method.
- Assistant
Let me look at ToolService.java.
- Assistant
Let me find and read the executeInferenceAndToolsLoop method in ToolService.
- Assistant
Let me view that section.
- Assistant
Let me check the ToolExecution and ToolExecutionResult classes to understand what data is available.
- Assistant
Let me also look at ToolExecutionResult to understand the structure:
- Assistant
Let me check the existing tests for ReturnBehavior to understand the expected behavior.
- Assistant
Let me also check the existing test files for tools and AiServices:
- Assistant
Now let me look at how the streaming AI service handles immediate tool returns.
- Assistant
Let me also look at the streaming service handling of immediate tool returns:
- Assistant
Let me also look at the streaming handling in DefaultAiServices.
- Assistant
Let me look at the streaming section more closely to see how it handles the immediate return case:
- Assistant
Let me look at the DefaultToolExecutor to see how the result() is set.
- Assistant
Let me check the DefaultToolExecutor to see how the result object is set in ToolExecutionResult.
- Assistant
Now I have a good understanding of the codebase. Let me also look at the TypeUtils to understand how to check if a type is a primitive.
- Assistant
Now I have a good understanding of the codebase. Let me plan the implementation:
ToolServiceResult.java: Add areturnBehaviorsfield to carry the list of return behaviors used during the immediate return decisionToolService.java: Remove theIllegalConfigurationExceptionthrow when!isReturnTypeResult, and pass the return behaviors list toToolServiceResultDefaultAiServices.java: Handle non-Resultreturn types whenimmediateToolReturnis true, implementing the four rules from the issue
Let me also check if there are any existing tests that test the current exception behavior to understand what needs to change.
- Assistant
Let me check the current error message thrown.
- Assistant
Let me search for the exact error message thrown when
!isReturnTypeResult: - Assistant
Let me see the full context around that code:
- Assistant
Let me also check if there's any validation that prevents non-Result return types when IMMEDIATE tools are configured.
- Assistant
Now I have a clear understanding. Let me implement the changes step by step. First, let me create a temporary test script to verify the current behavior and my changes:
- Assistant
Let me start implementing the changes. First, let me modify
ToolServiceResult.javato add a field for return behaviors: - Assistant
Let me scroll down to see the rest of the file:
- Assistant
Now let me add a
returnBehaviorsfield toToolServiceResult. I need to add the import and the field: - Assistant
I messed up the edit. Let me fix it by rewriting the file properly:
- Assistant
Now let me modify
ToolService.java. First, let me see the relevant section: - Assistant
Let me edit the section where the exception is thrown:
- Assistant
Now let me check if there are unused imports in ToolService.java after my changes:
- Assistant
Let me check if
illegalConfigurationis still used elsewhere in the file: - Assistant
Good,
illegalConfigurationis still used in other places, so the imports are still needed. Now let me modifyDefaultAiServices.javato handle non-Result return types whenimmediateToolReturnis true. Let me look at the exact section: - Assistant
Let me also check what imports are available at the top of the file:
- Assistant
Now let me modify the
DefaultAiServices.java. First, I need to add the import forReturnBehavior: - Assistant
I see the issue - I'm duplicating imports. Let me check the current state and fix it:
- Assistant
The imports are duplicated. Let me revert this file and redo the edit properly:
- Assistant
Now let me add just the new imports I need. First, let me add the static import for
TO_LLM: - Assistant
Good, the file is back to its original state. Now let me add the imports I need. First, add the static import for
TO_LLM: - Assistant
Now let me add the import for
ReturnBehaviorandToolExecution: - Assistant
Now let me add the import for
ToolExecution: - Assistant
Good. Now let me find the section where the immediate tool return is handled and modify it. Let me navigate to that area:
- Assistant
Now I need to modify the section that handles immediate tool return. Let me replace lines 352-364:
- Assistant
Now I need to fix a reference to
IMMEDIATEandIMMEDIATE_IF_LASTin the error message. These are not imported. Let me check what constants are available and fix the reference: - Assistant
I need to add static imports for
IMMEDIATEandIMMEDIATE_IF_LAST. Let me fix this: - Assistant
Now let me check if there are any tests that currently test the exception behavior that might break. Let me search for tests that test the exception when return type is not Result:
- Assistant
Let me search more broadly for tests related to immediate tool return and Result:
- Assistant
Let me check the streaming AiService code to see if it has similar restrictions:
- Assistant
Let me check the AiServiceTokenStream file:
- Assistant
Let me also check the streaming case in AiServiceTokenStream:
- Assistant
Good, the streaming case doesn't use
executeInferenceAndToolsLoop. Now let me try to compile the code to check for any issues: - Assistant
Good, the code compiles successfully. Now let me update the
ReturnBehavior.javajavadoc to reflect that IMMEDIATE/IMMEDIATE_IF_LAST are no longer restricted to Result return types only: - Assistant
I need to adjust the path - I'm in the langchain4j directory.
- Assistant
I see, the current directory is
/langchain4j/langchain4j. Let me navigate to the root and find the correct path for theReturnBehavior.javafile: - Assistant
Let me update the
ReturnBehavior.javajavadoc to reflect that IMMEDIATE/IMMEDIATE_IF_LAST are no longer restricted to Result return types only: - Assistant
Let me scroll down to see the rest of the file:
- Assistant
Now let me update the javadoc to reflect that IMMEDIATE/IMMEDIATE_IF_LAST are no longer restricted to Result return types only. First, let me update the class-level javadoc:
- Assistant
Now let me update the IMMEDIATE javadoc to remove the restriction:
- Assistant
Now let me also update the IMMEDIATE_IF_LAST javadoc to remove the restriction:
- Assistant
Wait, there's an extra
}at line 118. Let me check and fix this: - Assistant
Let me fix this by removing the extra
}: - Assistant
Wait, I see the javadoc closing
*/is missing. Let me fix this: - Assistant
Good. Now let me compile both modules to make sure everything compiles correctly:
- Assistant
Good, the compilation succeeded. Now let me run the existing tests to make sure I didn't break anything:
- Assistant
Good, the existing tests pass. Let me also run the LazyCalculationIntegrationTest:
- Assistant
Let me also run any existing tests that might be related to tool execution:
- Assistant
Let me run a broader set of tests to verify nothing is broken:
- Assistant
Let me wait a bit more for the tests to finish and check the result:
- Assistant
All tests pass. Now let me also think about edge cases with primitive types and autoboxing. Let me check if there's a need to handle autoboxing:
- Assistant
I need to handle autoboxing for primitive types. Let me update the code to handle this edge case:
- Assistant
Now I need to add the
isResultCompatibleWithReturnTypehelper method. Let me add it near the other helper methods: - Assistant
Let me add the helper method after
isImage: - Assistant
Good. Now let me compile and run the tests again to make sure everything still works:
- Assistant
Good, compilation succeeded. Now let me run the tests:
- Assistant
All tests pass. Now let me create a temporary test script to verify that my changes work as expected:
- Assistant
Let me copy this test file to the test directory and run it:
- Assistant
Let me run the test:
- Assistant
Let me look at the full test output to see which tests passed:
- Assistant
Let me update the test to remove the Object return type test case (since Object is not a valid return type for AI services):
- Assistant
The file got corrupted by my edit. Let me rewrite the entire test file:
- Assistant
Now let me run the test again:
- Assistant
All 4 tests pass! Now let me clean up the temporary test file before submitting:
- Assistant
Let me run the existing tests again to make sure everything still passes:
- Assistant
All tests pass. Now let me review all my changes to make sure they're complete and correct:
- Assistant
Let me review the full diff to make sure everything is correct:
- Assistant
The changes look good. Let me verify that there are no unused imports in my changes:
- Assistant
Let me also check if
IllegalConfigurationExceptionis still used in the ToolService.java file after my changes: - Assistant
Good,
IllegalConfigurationExceptionis still used in other places in ToolService.java. Let me also check if there are any other files that might reference the old error message: - Assistant
Good, the old error message is no longer present anywhere. Let me also check if there are any other tests that might be testing the old behavior:
- Assistant
Good, the old error message is no longer present anywhere else. Let me now create a summary of all changes made and submit them:
- Assistant
Let me also run the langchain4j-core tests to make sure the ReturnBehavior changes don't break anything:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.