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
I'll start by exploring the repository structure to understand the codebase and find the relevant files.
Assistant
Let me look at the AiServices.java and ToolService.java to understand the immediate return logic.
Assistant
Now let me see how the immediate return is handled after the loop returns. Let me find where
executeInferenceAndToolsLoopis called and howToolServiceResultis used.Assistant
Let me look at the ToolExecutionResult and ToolExecution classes to understand how tool results are stored.
Assistant
There's a
result()method returning the raw object. Let me look at the existing tests to understand the expected behavior.Assistant
Let me check how the
result(raw object) is set on ToolExecutionResult during tool execution.Assistant
So the
result()field is the raw object returned by the tool method. Good. Now let me understand the flow better and look at how the result object flows through to where it's used.Now let me understand the design. The PR referenced is for Quarkus. Let me look at the issue rules again:
- If ai service method return type is void, just return null
- If any tool executions are TO_LLM throw exception (IMMEDIATE_IF_LAST case)
Assistant
Let me check the streaming path for immediate return handling.
Assistant
Let me look at the ToolExecution class.
Assistant
Let me look at the broader DefaultAiServices context to see what's in scope.
Assistant
Let me check if ToolService is Internal/API.
Assistant
Let me look at the helper methods in DefaultAiServices for type handling.
Assistant
Let me look at TypeUtils for helpers.
Assistant
Now I have a thorough understanding. Let me implement the changes. First, modify
ToolService.executeInferenceAndToolsLoopto not throw for non-Result return types, and add the resolution logic.Assistant
I need to use the proper syntax. Let me open the file first.
Assistant
The open command needs the file as a positional arg and line as second. Let me check the usage. The usage says:
open [OPTIONS] [FILE] [LINE_NUMBER]. Soopen /path 472. But it errored. Let me try without line.Assistant
Now let me edit the block to remove the throw:
Assistant
Let me use the edit command via bash terminal with heredoc syntax:
Assistant
There's a duplicate line. Let me remove it:
Assistant
Now let me add the
resolveImmediateToolResultstatic method aftershouldReturnImmediately:Assistant
Now I need to add the imports for
TypeandTypeUtilsin ToolService.java:Assistant
Now add the TypeUtils import:
Assistant
Let me verify the end of the new methods:
Assistant
The
boxmethod is missing its closing brace. Let me fix that:Assistant
The box method got corrupted. Let me rewrite it cleanly:
Assistant
Now let me modify
DefaultAiServicesto handle the non-Result immediate return case:Assistant
Let me verify
ToolServiceis imported in DefaultAiServices:Assistant
Need to add the
ToolServiceimport:Assistant
Now let me update the
ReturnBehaviorJavadoc to reflect the new behavior:Assistant
Let me update the class-level Javadoc too:
Assistant
Now let me try to compile the affected modules to verify there are no errors:
Assistant
Compilation succeeds. Now let me write a temporary test to verify the new behavior. Let me look at the ChatModelMock API first:
Assistant
Let me create a temporary test to verify the behavior. First check the test dir:
Assistant
All 6 tests pass. Now let me run the existing related tests to ensure I didn't break anything:
Assistant
All existing tests pass. Let me run a broader set of service unit tests to make sure nothing broke:
Assistant
All tests pass. Now let me remove the temporary test file and review the final diff:
Assistant
Now let me review the diff to ensure everything is correct.
Assistant
Let me check if there's a checkstyle/errorprone config that might flag the now-unused
isReturnTypeResultparameter:Assistant
Let me verify the
illegalConfigurationstatic import exists in ToolService:Assistant
Let me do a final compile of both modules to confirm everything is clean:
Assistant
Let me confirm the temp test is fully removed (no leftover compiled class):
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.