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 AiService and tool execution handling.
- Assistant
Let me look for AiService related files.
- Assistant
Let me find the core AiService implementation files.
- Assistant
Let me search for ReturnBehavior and tool execution related files.
- Assistant
Let me look at the key files to understand the current behavior and what needs to change.
- Assistant
Let me look at the ReturnBehavior enum and related files.
- Assistant
Let me examine the key files that handle tool execution and the ReturnBehavior enum.
- Assistant
Let me look at the DefaultAiServices class which likely handles the tool execution logic.
- Assistant
The file was truncated. Let me look at the tool service execution logic and understand the immediate return behavior.
- Assistant
Let me see the full content around the immediate tool return logic.
- Assistant
Now I need to understand the
ToolServiceResultclass and howexecuteInferenceAndToolsLoopworks. Let me also look atToolService.java. - Assistant
Let me look at the ToolServiceResult class to understand its structure.
- Assistant
Let me look at the existing tests to understand the current behavior and what needs to change.
- Assistant
Let me look at the
executeInferenceAndToolsLoopmethod in ToolService. - Assistant
I need to see the full implementation of the
executeInferenceAndToolsLoopmethod. - Assistant
Now I understand the issue. Let me check the referenced PR to understand the expected behavior better. But first, let me look at the existing tests for immediate return tools.
- Assistant
Let me now create a test script to reproduce the current behavior and understand the issue better. First, let me look at the structure of the
DefaultAiServicesclass to understand how it currently handles return types. - Assistant
Let me create a test script to reproduce the current behavior and understand what needs to change.
- Assistant
Let me look at how the existing tests work and how the codebase is structured. Let me find the test classes first.
- Assistant
We're in /tmp/test-immediate now. Let me go back to the main repo.
- Assistant
Let me look at the exact code sections more carefully to understand what needs to change.
- Assistant
Let me look at the exact code that throws the exception:
- Assistant
Let me look at ToolExecution and ToolExecutionResult to understand how tool results are stored.
- Assistant
Now let me look at the
ToolExecutionResultclass to understand how tool results are stored. - Assistant
Let me look at the
TypeUtilsclass to understand how to check for primitive types and assignability. - Assistant
Let me continue reading the TypeUtils class to understand how to check if a type is primitive.
- Assistant
Let me look at how the return type is determined and used in DefaultAiServices.
- Assistant
Now I have a good understanding of the codebase. Let me create a plan:
-
In
ToolService.java: Remove the restriction that throwsIllegalConfigurationExceptionwhen the return type is notResult. Instead, allow immediate return for any return type. -
In
DefaultAiServices.java: Update the code that handlestoolServiceResult.immediateToolReturn()to handle non-Result return types based on the rules in the issue:- If return type is void → return null
- If all tool executions have null results and return type is nullable (not primitive) → return null
- If there's one non-null tool execution result that can be resolved to the return type → return that object
Let me start by modifying
ToolService.java: -
- Assistant
Now I need to modify the code in
ToolService.javato remove the restriction. Let me update lines 472-477: - Assistant
I see there's a duplicate now. Let me fix that by removing the duplicate code:
- Assistant
I need to remove the duplicate code that's left over. Let me remove lines 482-490:
- Assistant
Now I need to modify
DefaultAiServices.javato handle the immediate return for non-Result return types. Let me look at the current code again: - Assistant
Wait, I ran
sedon the wrong file. Let me get the correct file content: - Assistant
The current directory says we're in the ToolService.java file. Let me open the correct file:
- Assistant
Now I need to modify the code in
DefaultAiServices.javato handle immediate return for non-Result return types. Let me edit the file: - Assistant
I need to add the import for
IllegalConfigurationException.illegalConfigurationat the top of the file. Let me check the imports: - Assistant
Good, the import for
IllegalConfigurationException.illegalConfigurationis already there (line 10). Let me check if the import forCollectorsis there or if I need to add it: - Assistant
I need to add the import for
java.util.stream.Collectors. Let me check the imports section: - Assistant
I need to add the import for
java.util.stream.Collectors. Let me add it: - Assistant
Now let me update the code to use the imported
Collectorsinstead of the fully qualified name: - Assistant
I see there's a duplicate line. Let me fix it by removing the duplicate:
- Assistant
I still have a duplicate. Let me remove the duplicate comment line:
- Assistant
This is getting messy with multiple edits. Let me clean it up by replacing the whole problematic section:
- Assistant
The edits have created a mess with multiple duplicate lines. I need to reset the file and start fresh. Let me reset the file from git:
- Assistant
Let me also reset ToolService.java and start fresh with both files:
- Assistant
Good, now let me start fresh. Let me look at the current files again:
- Assistant
Let me use the open command on the correct file first.
- Assistant
Now let me make the change to ToolService.java properly. I need to remove the check that throws IllegalConfigurationException for non-Result return types (lines 473-477):
- Assistant
Let me reset and use sed instead for cleaner edits:
- Assistant
Now let me use sed to make the change cleanly. First, let me see the exact lines:
- Assistant
Now let me use sed to remove the lines that check for
isReturnTypeResult: - Assistant
Let me verify the change:
- Assistant
Good! Now the ToolService.java file is updated correctly. The restriction that throws IllegalConfigurationException for non-Result return types is removed.
Now let me modify DefaultAiServices.java to handle immediate return for non-Result return types. Let me open that file:
- Assistant
I need to add the import for
Collectorsand then modify the logic. Let me use sed to add the import: - Assistant
Now I need to modify the logic in DefaultAiServices.java to handle immediate return for non-Result return types. Let me replace the block at lines 349-361:
- Assistant
Now let me replace the block to handle both Result and non-Result return types:
- Assistant
Now I need to replace lines 349-361 with the content from the replacement file. Let me do this with sed:
- Assistant
Let me verify the changes are correct:
- Assistant
I see there's an extra
}at the end. Let me remove it: - Assistant
Let me check the indentation and structure more carefully. Let me look at a larger portion:
- Assistant
I see the issue - there's an extra closing brace. Let me remove it:
- Assistant
Let me verify the file structure is correct now:
- Assistant
There's still an extra closing brace. Let me remove it:
- Assistant
There's still an extra
}between line 403 and 404. Let me look more carefully: - Assistant
There are two closing braces, but only one is correct. Let me remove the extra one:
- Assistant
Let me verify the structure is correct now:
- Assistant
Good! The structure looks correct now. Let me verify the changes compile by running some tests. First, let me check if there are any existing tests related to this feature:
- Assistant
Let me check if there are any tests that check for the IllegalConfigurationException that we just removed:
- Assistant
Good, no tests are expecting that error message. Now let me also update the ReturnBehavior enum's Javadoc to reflect that it's no longer limited to
Resultreturn types: - Assistant
I need to update the Javadoc in ReturnBehavior.java to reflect the changes. Let me update the class-level Javadoc first:
- Assistant
Now let me update the Javadoc for ReturnBehavior to reflect that it's no longer limited to Result return types:
- Assistant
Now let me replace the old Javadoc with the new one:
- Assistant
Let me verify the Javadoc change:
- Assistant
Good! Now I need to update the Javadoc for the
IMMEDIATEandIMMEDIATE_IF_LASTenum values. Let me look at them: - Assistant
Now let me update the Javadoc for
IMMEDIATEto remove the restriction: - Assistant
Let me update line 90 to remove the restriction:
- Assistant
Let me also update the
IMMEDIATE_IF_LASTJavadoc. Let me look at that section: - Assistant
Now let me find and update the restriction for IMMEDIATE_IF_LAST:
- Assistant
Let me update line 119:
- Assistant
Let me verify the changes are correct:
- Assistant
The formatting got messed up. Let me reset this file and try again with better formatting:
- Assistant
Let me use Python to update the ReturnBehavior.java file properly:
- Assistant
Let me try python instead:
- Assistant
Let me use the replace command instead:
- Assistant
Let me replace the class-level javadoc first (lines 47-51):
- Assistant
Good! Now let me replace the IMMEDIATE javadoc:
- Assistant
Good! Now let me update the IMMEDIATE_IF_LAST javadoc:
- Assistant
Now let me verify the changes compile by building the project. Let me run the existing tests:
- Assistant
Good! The code compiles. Now let me run the existing tests to make sure I didn't break anything. Let me run the ReturnBehaviorCombinationsTest first:
- Assistant
Good! The ReturnBehaviorCombinationsTest passes. Let me also run the ProgrammaticCreatedImmediateReturnToolTest:
- Assistant
Good! Let me now run the tests that might be related to the immediate return behavior:
- Assistant
Let me check if there are any tests in the abstract test class that might test the error case:
- Assistant
Let me look at the test that checks for the error message:
- Assistant
I see there's a test that expects an
IllegalConfigurationExceptionwhen using an immediate tool on a service that doesn't returnResult. This test will now fail because we removed that restriction. However, in this specific test case, the tool returnsint(37 + 87 = 124) but the service returnsString, so according to our new rules, it should still throw an exception. Let me check what the tool returns: - Assistant
Let me look at the definition of
ImmediateToolWithPrimitiveParameters: - Assistant
Let me check what the ChatModelMock is configured to return in this test:
- Assistant
I see the test expects an exception when using an immediate tool on a service that doesn't return
Result. This test needs to be updated because the behavior has changed. Let me update the test to reflect the new behavior: - Assistant
I see that the test
should_throw_using_immediate_tool_on_service_not_returning_Resultneeds to be updated. This test was testing the old behavior where an exception was thrown when using an immediate tool on a service that doesn't returnResult. After our changes, the behavior is different:- If the return type is
voidorVoid,nullis returned - If all tool executions returned
nulland the return type is nullable (not primitive),nullis returned - If there is exactly one non-null tool execution result that is assignable to the return type, that result is returned directly
- Otherwise, an
IllegalConfigurationExceptionis thrown
In this test case:
- Tool returns
int(primitive) - Service returns
String - These types are incompatible
- So an exception should still be thrown, but with a different message
I need to update the test to reflect the new behavior. Let me update it:
- If the return type is
- Assistant
Now let me run the tests to see if they pass. First, let me check if there are any compilation errors:
- Assistant
Good, no compilation errors. Let me now check if there are any other tests that might be affected by our changes:
- Assistant
Let me also check the LazyCalculationIntegrationTest to see if it needs updating:
- Assistant
Let me also check the StreamingProgrammaticCreatedImmediateReturnToolTest:
- Assistant
These tests look fine. They're testing the streaming path which I haven't changed. Let me now run the tests to see if they pass:
- Assistant
Good! The tests pass. Let me also check if there are any other callers of the
executeInferenceAndToolsLoopmethod to see if we can remove theisReturnTypeResultparameter: - Assistant
Good, there are only two places where
executeInferenceAndToolsLoopis called. Since we removed the check forisReturnTypeResultinToolService.java, the parameter is no longer used there. However, I'll leave it as is to avoid breaking changes.Let me run a broader set of tests to make sure we didn't break anything:
- Assistant
Good! The tests pass. Let me now run the tests for the entire
langchain4jmodule to make sure we didn't break anything: - Assistant
Let me wait for the tests to complete and check the results:
- Assistant
Excellent! All tests pass. Let me also check if there are any other places in the codebase that reference the old behavior:
- Assistant
Let me also check if there are any other places in the codebase that might need to be updated:
- Assistant
Good, no other places in the codebase are throwing
IllegalConfigurationExceptionrelated toResultorIMMEDIATE. Let me now run the full test suite one more time to make sure everything is working correctly: - Assistant
Let me wait for the tests to complete and check the final results:
- Assistant
All tests pass (1385 tests, 0 failures, 2 skipped). Let me now create a summary of the changes and submit them:
- Assistant
Let me check the diff to make sure all the changes are correct:
- Assistant
The changes look good. Let me now submit the changes. But first, let me verify the changes one more time by looking at the key files:
- Assistant
Now let me submit the changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.