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.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /langchain4j, current file: none) bash-$
- Assistant
I'll start by exploring the repository structure and locating the AiService / ReturnBehavior.IMMEDIATE related code so I can implement non-Result return types.
- Assistant
Next I'll inspect ReturnBehavior, ToolService, and related tests to see how IMMEDIATE is handled today.
- Assistant
Let me look at the ToolService executeInferenceAndToolsLoop method more carefully, and DefaultAiServices to understand the return type handling.
- Assistant
Let me look at DefaultAiServices to see how the result is processed and what's currently done with return types.
- Assistant
Looking at the immediate-return path in DefaultAiServices and the existing IMMEDIATE tests for context.
- Assistant
Checking git history and any stubs or TODOs for this feature.
- Assistant
Inspecting ToolExecution/result types and how streaming handles immediate returns.
- Assistant
Looking for tests that assert non-Result IMMEDIATE failures and reviewing TypeUtils.
- Assistant
Inspecting the non-Result IMMEDIATE failure test and related IMMEDIATE handling logic.
- Assistant
Checking the streaming handler for the immediate-return path and scanning for related helper utilities.
- Assistant
Reading the tools docs around IMMEDIATE return type constraints.
- Assistant
Checking other callers of this API and how TypeUtils handles primitives.
- Assistant
Checking void-tool results and imports used for configuration errors.
- Assistant
// from DefaultToolExecutor around line 150-195 - Assistant
Checking how ToolExecutor builds ToolExecutionResult:
- Assistant
Implementing the changes: updating ToolService, ToolServiceResult, DefaultAiServices, docs, and ReturnBehavior.
- Assistant
Updating
ToolServiceResultto carry immediate-return tool execution metadata. - Assistant
Updating ToolService's immediate-return path next.
- Assistant
I need to update the when shouldReturnImmediately block. Also check if Result import is only for the exception message - might still need it for something else.
- Assistant
Updating the immediate-return block in ToolService.
- Assistant
Now replace the immediate return block.
- Assistant
Need to navigate to the line first or use sed.
- Assistant
Now I can replace.
- Assistant
Adding a static method on ToolService to map tool results to the AI Service return type.
- Assistant
Cleaning up FQCNs and adding proper imports.
- Assistant
need to goto first
- Assistant
Let me do the replace now.
- Assistant
Now fix the fully qualified names in resolveImmediateToolReturn.
- Assistant
Let me fix the FQCN usage.
- Assistant
Updating DefaultAiServices to handle non-Result immediate returns.
- Assistant
I need to open DefaultAiServices
- Assistant
Update the immediate return handling block.
- Assistant
Need to add ToolService import to DefaultAiServices.
- Assistant
Add ToolService import.
- Assistant
Add import for ToolService
- Assistant
Updating ReturnBehavior Javadoc and documentation.
- Assistant
I'll update the Javadoc comprehensively.
- Assistant
Updating the tutorials documentation next.
- Assistant
Let me read those sections and update them.
- Assistant
I'll update the docs with the new rules.
- Assistant
I'll replace the note section and the IMMEDIATE_IF_LAST line, and expand the examples.
- Assistant
Update the IMMEDIATE_IF_LAST note.
- Assistant
goto first
- Assistant
Nowreplace
- Assistant
Writing a temporary unit test for the new behavior and compiling the module.
- Assistant
HOME is not set. Let me set it.
- Assistant
Compilation succeeded. Running the temporary test.
- Assistant
One failure is from null String→ToolExecutionResultMessage, not the new mapping. Checking how void vs null String results are encoded:
- Assistant
Adjusting the temp test (null-String→message is pre-existing) and re-running.
- Assistant
Fixing the typo in the temporary test.
- Assistant
Also need to check argument names - DefaultToolExecutor uses method parameter names which might be a,b not arg0,arg1. Looking at how tests usually call:
- Assistant
The output got cut off - let me check Insed test results.
- Assistant
All 9 tests passed. Removing the temporaryもたらtest and running existing related unit tests.
- Assistant
Checking Utils.copy null-safety for a unset field.
- Assistant
Fixing
@sincetags to match the current version. - Assistant
Updating
@sinceto 1.16.0 for the new APIs. - Assistant
Update ToolService resolveImmediateToolReturn @since
- Assistant
Fix @since
- Assistant
Also check
@Toolannotation javadoc if it mentions Result requirement. - Assistant
Doing a final compile to confirm everything builds’ cleanly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →