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 exploring the repository structure to understand what we're working with. This is a LangChain4j repository (likely the main LangChain4j repository, not the Quarkus extension). The issue is about supporting non-Result return types for AiService when tool execution is IMMEDIATE.
Let me first understand the repository structure and find relevant files.
Assistant
Let me focus on finding the relevant AiService files, especially related to tool execution and return types
Assistant
Let me search more specifically for the AiService implementation and the ReturnBehavior enum
Assistant
Let me look at the key files. First, the ReturnBehavior enum and the main AiServices implementation
Assistant
Now let me look at the AiServices.java - this is likely the main file where the logic is implemented. Let me also look at the ToolService and related classes.
Assistant
Let me first look at the existing code for how immediate returns are handled. Let me search for relevant sections:
Assistant
Let me search for where the actual handling of immediate results happens. It seems like
ToolServicemight be key.Assistant
Let me open ToolService.java to see the full implementation:
Assistant
Let me look at the open command more carefully. It seems it needs a different format. Let me just cat the file.
Assistant
The file was truncated. Let me see the full file, especially the
executeInferenceAndToolsLoopmethod:Assistant
Now let me see the
shouldReturnImmediatelymethod and also see theToolServiceResult:Assistant
Now let me look at
ToolServiceResultAssistant
Now let me find where
executeInferenceAndToolsLoopis called inAiServices.javaand how the result is handled:Assistant
Let me search more broadly
Assistant
Let me look at the relevant sections of
DefaultAiServices.java:Assistant
Let me look at
ToolExecutionResultandToolExecutionto understand the resultAssistant
Let me look at those files.
Assistant
Now let me also look at ToolExecution:
Assistant
Now let me look at the DefaultToolExecutor to understand how tool results are returned:
Assistant
Let me look at how the executor returns the result:
Assistant
Let me look at existing tests for immediate return tool behavior
Assistant
Let me look at these test files to understand the expected behavior:
Assistant
Let me look at the ReturnBehaviorCombinationsTest:
Assistant
Let me look at the full DefaultAiServices.java, particularly around the key section
Assistant
Let me see the imports and the first part of DefaultAiServices
Assistant
Let me see more of the build method:
Assistant
Let me search for all callers of
executeInferenceAndToolsLoopAssistant
Let me check if there's a streaming equivalent of this logic
Assistant
Let me check the streaming side for similar logic:
Assistant
Let me check for validation that would prevent non-Result types with immediate tools
Assistant
Let me look at the
Resultclass:Assistant
Let me reproduce the issue by creating a small test:
Assistant
Let me understand the build system and try to run a test:
Assistant
Let me run the existing tests related to immediate return behavior to see the current state
Assistant
Now let me also look at the
TypeUtilsfor helper methods likegetRawClassandtypeHasRawClass:Assistant
Let me also check the existing tests for AiServices with tools to see if there are non-Result tests:
Assistant
Now let me implement the changes. First, let me modify
ToolService.javato remove theisReturnTypeResultparameter and the throw:Assistant
Now let me make the changes to
ToolService.java. First, remove theisReturnTypeResultparameter and the throw blockAssistant
Now let me edit the method signature to remove the
isReturnTypeResultparameter:Assistant
Let me use replace instead
Assistant
I need to navigate to that line first. Let me use goto.
Assistant
Now let me remove the
isReturnTypeResultparameterAssistant
Now remove the throw block that checks
isReturnTypeResultAssistant
Let me check if
Resultis still used anywhere in ToolService.javaAssistant
Now let me remove the unused
Resultimport from ToolService.javaAssistant
The replace command seems to be sensitive about whitespace. Let me try a different approach:
Assistant
Now let me update
DefaultAiServices.java. Let me first open itAssistant
Let me navigate to the relevant section
Assistant
Now let me implement the changes to
DefaultAiServices.java. First, let me add the needed imports:Assistant
Let me use sed to add the imports and modify the code directly
Assistant
Let me verify the imports and also add
ToolExecutionandObjectsAssistant
Now let me add the
ToolExecutionimport (forToolExecution::resultObject) andObjects:Assistant
Now add
Objects:Assistant
Now let me update the call to
executeInferenceAndToolsLoopby removing theisReturnTypeResultargument. Let me first navigate to the right lineAssistant
Let me verify the edit was applied correctly
Assistant
The multiline sed didn't work. Let me use a Python script for these complex edits
Assistant
Let me try python instead
Assistant
Let me try using a different approach with sed. First, let me see the exact current state of the file at the relevant lines
Assistant
Let me use a Java program to do the edits. That should be available since this is a Java project:
Assistant
Let me compile and run the Java editor
Assistant
Let me verify the changes were applied correctly
Assistant
Good, the changes look correct. Now let me compile to ensure there are no compilation
Assistant
Compilation succeeded. Let me now run the existing tests to make sure nothing is broken
Assistant
Let me also run the broader tools integration tests:
Assistant
Let me also run the unit tests for AiServices
Assistant
Let me also run a broader set of tests to make sure we haven't broken anything:
Assistant
All tests pass. Let me also check the streaming side to see if it needs similar changes
Assistant
Let me verify the
illegalConfigurationmethod signature to make sure our call matches:Assistant
Now I should update the
ReturnBehaviorjavadoc to reflect the new behavior. Let me look at itAssistant
Let me see the full javadoc comment:
Assistant
Let me update the Javadoc to reflect the new behavior
Assistant
Let me see the last part of IMMEDIATE_IF_LAST:
Assistant
Let me update the Javadoc to reflect the new behavior. First, the class-level Javadoc:
Assistant
Now update the IMMEDIATE Javadoc (line 93
Assistant
Now update the IMMEDIATE_IF_LAST Javadoc
Assistant
Let me also check the Javadoc for AiServices to see if it mentions the Result requirement
Assistant
Let me compile the full project to make sure everything works
Assistant
Let me run a broader set of tests to confirm nothing is broken
Assistant
All 1385 tests pass. Let me now check the git diff to review all changes
Assistant
Let me verify the streaming code path doesn't need changes
Assistant
The streaming path doesn't use these methods. Let me clean up the temp files and do a final review
Assistant
Let me also run the langchain4j-core tests to make sure the Javadoc changes didn'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.