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
Now let's view the full method signature area and surrounding context to plan edits precisely.
Assistant
Good, imports are fine. Now let's implement the changes to ToolService.java.
Assistant
I'll use the
editcommand instead.Assistant
Now let's add tracking of current-round tool executions and rewrite the immediate-return block.
Assistant
Now let's add the helper methods
resolveImmediateReturnValueandisAssignableToReturnType, plus wrapper mapping, right after theshouldReturnImmediatelymethod.Assistant
Good, that's fixed. Now I need to add imports (
java.lang.reflect.Type,TypeUtils.getRawClass,TypeUtils.typeHasRawClass) since I used them but they may not be imported yet.Assistant
Now let's add the
Typeimport.Assistant
Now let's update
ToolServiceResultto add theimmediateReturnValuefield.Assistant
Now let's update DefaultAiServices.java to pass
returnTypeand handle the new field.Assistant
I need to remove the duplicated block (lines 367-370).
Assistant
Good, that's fixed now (small extra blank line at 367, harmless). Let's clean that extra blank line and then compile.
Assistant
Now let's compile the module to check for errors.
Assistant
Compilation succeeded with no errors. Let's now run relevant tests.
Assistant
Good, those pass. Now let's run the broader
AiServicesIT,AbstractAiServiceWithToolsITrelated tests (the one with existing exception expectation), andAiServicesWithToolsIT.Assistant
All 6 scenarios pass. Now let's remove the temp test file and run the full existing test suite for the tool/service related packages to make sure nothing broke.
Assistant
That output is just expected retry warnings within a test (that's normal test output logged intentionally, not necessarily a failure). Let's check the actual final summary.
Assistant
All tests pass (BUILD SUCCESS, 1247 tests, 0 failures). Let's also run the full test suite for
langchain4jmodule to be thorough, and double checkgit difffor review.Assistant
Good, no leftover test files. Let's view the full diff to review.
Assistant
Let's update this javadoc to reflect the new behavior for non-Result return types.
Assistant
There's a duplicate
*/at line 68-69. Let's fix, and also update the "Only allowed on AI services returning Result" lines at 98 and 127.Assistant
Now update lines 97 and 126 wording.
Assistant
This is just a javadoc-only change (no code). Let's recompile everything to make sure it's all fine.
Assistant
Now let's rerun the full test suite one more time to ensure everything is stable after the doc changes (no functional change expected, but let's be safe).
Assistant
All 1247 tests pass, 0 failures/errors. Let's do a final review of the diff.
Assistant
The changes look complete and clean. Let's go through the checklist.
Assistant
This all looks correct. Now let me address the checklist explicitly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.