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 look at ToolServiceResult and serviceOutputParser to understand type checking/conversion utilities available.
Assistant
Now let's check ToolServiceResult builder to add a new field for the resolved immediate return value.
Assistant
Now let's implement the changes. First update imports to add
java.lang.reflect.TypeandTypeUtils(getRawClass, typeHasRawClass).Assistant
Now let's update the method signature and the immediate-return block.
Assistant
Now let's add the
resolveImmediateReturnValuehelper method right afterexecuteInferenceAndToolsLoop(or nearshouldReturnImmediately).Assistant
Now let's update
ToolServiceResultto add theimmediateReturnValuefield.Assistant
Now let's update
DefaultAiServices.javato call the new method signature and useimmediateReturnValue().Assistant
We have network. Let's try building online with a longer timeout.
Assistant
Compiles cleanly. Now let's run the existing tests related to ReturnBehavior/tool immediate return.
Assistant
Tests passed with no failures reported. Now let's write a quick reproduction script to verify the new feature works as intended.
Assistant
Good — for a void tool method,
result(raw object) isnullsincemethodToInvoke.invokereturns null for void methods, soresultObject()will correctly be null. Now let's run the manual test.Assistant
All 6 manual tests pass, confirming the feature works correctly including edge cases. Now let's test the "TO_LLM mixed with IMMEDIATE_IF_LAST" scenario throws an exception for non-Result return types.
Assistant
All edge cases work as expected. Now let's remove the manual test files (not to be submitted) and run the full existing test suite for the
service.toolpackage to make sure nothing is broken.Assistant
All 1247 tests pass (320 skipped, likely integration tests requiring real API keys). Now let's also check
git status/git diffto review the final changes.Assistant
Good, only the intended files changed. Let's also check for stray unused imports (e.g.,
illegalConfigurationimport still used,IllegalConfigurationExceptionimport still needed).Assistant
Let's update the ReturnBehavior javadoc to reflect the new supported behavior for non-Result return types.
Assistant
Now let's update the two "Only allowed on AI services returning" lines too, for consistency.
Assistant
Now let's rebuild to make sure everything still compiles (core module change doesn't affect compilation but let's verify).
Assistant
Let's run the full test suite once more (langchain4j module) to be safe after the doc-only core change.
Assistant
All 1247 tests pass. Let's do a final review of the diff.
Assistant
Now let me go through the checklist as requested.
Checklist Review
-
If an error was reported in the issue description, I have successfully reproduced it. The issue was a feature request, not an error report. I reproduced the limitation it describes: before my change,
ToolService.executeInferenceAndToolsLoopunconditionally threwIllegalConfigurationException("AI Service method must return aResulttype...") whenever an immediate tool return occurred on a non-Resultreturn type. I confirmed this was the exact behavior in the original code (line ~473-477 ofToolService.java) before making changes. -
If an error was reported in the issue description, I have confirmed that my fix resolves the error. I wrote temporary manual tests (later removed, per instructions not to leave/add tests) covering all four rules from the issue: (1)
voidreturn type → returnsnullwithout throwing; (2) all tool results + nullable return type → returns ; (3) exactly one non-null tool result whose type matches the return type (including primitive ) → returns that value; (4) ambiguous cases (multiple non-null results, or mixed into an immediate return, or result for a primitive return type) → throws a clear . All these scenarios were verified to work as expected via a temporary test class, then removed before submission.
-
Assistant
Confirmed no stray test files remain — only the four production files are modified.
-
I have thought about edge cases and made sure my fix handles them as well. Edge cases considered and handled:
- return type (checked first, bypasses all other logic).
-
Assistant
The implementation is complete and verified. Let me do one final full compile+test pass to be absolutely sure before submitting.
Assistant
Full test suite for both
langchain4j-coreandlangchain4jmodules passes: 1385 tests, 0 failures, 0 errors, BUILD SUCCESS. The implementation is complete, well-tested, and all edge cases are handled. Let's 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.