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. This is a Java project (LangChain4j), and the issue mentions Quarkus integration.
Let me explore the repository structure first.
Assistant
Let me look at the structure more and find relevant files related to AiService and tool execution.
Assistant
Let me find the AiService-related files, particularly around tool execution and IMMEDIATE return behavior.
Assistant
Let me look at the key files. The main ones I should look at:
ToolService.java- where tool execution happensToolServiceContext.java- context for tool serviceAiServices.java- the main AiService creationReturnBehavior.java- the enum for return behaviorsAiServiceTool.java- the tool interface
Let me also search for "IMMEDIATE" to understand the current behavior.
Assistant
Now let me look at the key files to understand the current implementation
Assistant
The file was truncated. Let me see the rest of the ToolService.java, and also look at ToolServiceContext.java and AiServices.java.
Assistant
Let me see the ToolServiceContext and the full ToolService.java.
Assistant
Now let me look at the AiServices.java to understand how tool results are handled:
Assistant
Let me search for relevant parts in AiServices.java related to IMMEDIATE, Result, and return types
Assistant
Let me search more specifically for the relevant handling logic
Assistant
Let me read the rest of the ToolService.java (around line 200-400) to see the execute method:
Assistant
Let me continue reading the executeInferenceAndToolsLoop
Assistant
Let me find where
isReturnTypeResultis computed and howexecuteInferenceAndToolsLoopis calledAssistant
Let me look at DefaultAiServices.java to understand the full flow:
Assistant
Let me see the critical section of DefaultAiServices.java where the tool results are processed
Assistant
Let me look at ToolServiceResult.java
Assistant
Let me look at ToolExecution and understand the tool execution result structure
Assistant
Let me look at ToolExecutionResult.java
Assistant
Let me look at the test files to understand the test
Assistant
Let me look at tests and understand what would need to change. Also let me check the streaming handler since it also references ReturnBehavior:
Assistant
Let me look at the streaming handler too
Assistant
Let me see the streaming handler to check if it also needs changes:
Assistant
Let me see more of the streaming
Assistant
Let me check what the Quarkus PR does for reference (it's mentioned in the issue description
Assistant
Let me look at TypeUtils and understand available utility methods:
Assistant
Let me look at the streaming handler to see if it also needs changes:
Assistant
Now let me look at the full context in the streaming handler and the full DefaultAiServices. Let me first look at how the streaming handler is invoked:
Assistant
Let me look at how streaming is set up in DefaultAiServices.java
Assistant
The streaming might be handled in a different class. Let me search for where the streaming handler is used.
Assistant
Let me look at AiServiceTokenStream.java to understand the streaming
Assistant
Now I have a good understanding. Let me first look at existing tests to make sure I understand the current behavior and then implement the fix. Let me first find tests related to this behavior
Assistant
Let me look at the test that validates the current IllegalConfigurationException for non-Result types
Assistant
Let me search for the specific test that validates the exception for non-Result types
Assistant
Let me search for where the exception message is constructed
Assistant
Let me check if there are tests that trigger this specific error
Assistant
Let me look at the LazyCalculationIntegrationTest:
Assistant
Now let me look at the full imports of DefaultAiServices
Assistant
Now I have a clear picture. Let me implement the changes. I'll start with ToolService.java to remove the exception
Assistant
Now let me make the changes. First, edit ToolService.java to remove the exception throw
Assistant
Good. Now let me update DefaultAiServices.java. First, let me see the exact section I need to modify:
Assistant
Now let me modify DefaultAiServices.java. First, I'll add the necessary imports, then modify the logic
Assistant
Something went wrong with the edit. Let me revert the file first
Assistant
Let me try a more targeted approach. First, let me open the file and check the imports
Assistant
Good, the file is restored. Let me add the needed imports using
replaceAssistant
Now add the import for
ReturnBehaviorAssistant
Now add the import for
ToolExecutionAssistant
Now let me find and replace the immediate return handling block. Let me find the exact
Assistant
Let me see the exact block to replace
Assistant
Now let me replace the immediate return block (lines 353-366) with the new logic
Assistant
Now I need to add the
handleImmediateToolReturnmethod. Let me add it after `fireEventAndAssistant
There's a duplicate
fireEventAndReturn. Let me fix this by removing the duplicateAssistant
The duplicate still exists. Let me fix this more carefully. Let me look at the exact state:
Assistant
Let me fix this by replacing the duplicate methods with a single correct one
Assistant
Let me verify that
Resultis accessible (it should be since it's in the same package). Let me also check if there are any compilationAssistant
Good. Now let me try to compile to check for errors
Assistant
Compilation succeeded. Now let me run the relevant tests
Assistant
Let me try with fully qualified test names
Assistant
Let me try running tests without the
-amflagAssistant
All tests pass. Let me now run the broader AiService-related tests
Assistant
Let me now run the AbstractAiServiceWithToolsIT test
Assistant
Now let me update the ReturnBehavior.java javadoc to reflect the new behavior
Assistant
Let me see the full ReturnBehavior.java javadoc
Assistant
Let me see the rest
Assistant
Let me see the javadoc for IMMEDIATE and IMMEDIATE_IF_LAST
Assistant
Now let me update the ReturnBehavior.java javadoc. First, let me update the class-level description
Assistant
Let me fix the leftover text on line 13, and update the outdated javad
Assistant
Now let me update the class-level javadoc restriction and the individual enum javado
Assistant
Now let me update the javadoc for IMMEDIATE and IMMEDIATE_IF_LAST
Assistant
Let me see the exact lines to replace
Assistant
Let me use the correct indentation:
Assistant
Now update the IMMEDIATE_IF_LAST javadoc
Assistant
Let me compile and run tests again
Assistant
Compilation succeeded. Let me run the tests
Assistant
All tests pass. Let me now run the broader test suite to make sure nothing
Assistant
All 204 tests pass. Let me verify the full changes with
git diffAssistant
I notice I accidentally removed a line from the class-level javadoc. Let me fix that:
Assistant
Let me use a simpler approach to fix the missing line:
Assistant
Let me add back the missing line describing the default TO_LLM behavior
Assistant
Let me compile and run the tests one final time to confirm everything is good
Assistant
All 83 tests pass. Let me verify the final diff
Assistant
Let me also run one more comprehensive test run to make sure nothing is broken
Assistant
Let me run the full test suite for the langchain4j module
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.