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 existing tests for the "must return Result" exception to understand the message format expected in tests.
Assistant
Now let's view lines around 400-490 fully for editing.
Assistant
Now let's add necessary imports and modify the method.
Assistant
Let's use
open/editspecial commands instead.Assistant
Now add
java.lang.reflect.Typeimport:Assistant
Now let's modify the
executeInferenceAndToolsLoopmethod signature and the immediate-return handling block.Assistant
No hidden chars. Let's just use
editcommand with line numbers directly.Assistant
Now let's find the updated line numbers for the loop body and immediate-return block.
Assistant
Now let's edit lines 451-487 to add round tracking and new resolution logic.
Assistant
There's leftover duplicate lines 495-498. Let's remove them.
Assistant
Good, now let's add the
resolveImmediateReturnValuehelper method right aftershouldReturnImmediately.Assistant
Need a blank line before the next javadoc comment at 621.
Assistant
Good. Now let's update
ToolServiceResultto addimmediateReturnValuefield.Assistant
Now add getter, update equals/hashCode/toString, and builder.
Assistant
Now update the Builder class to add the field and method.
Assistant
Leftover duplicate lines at the end. Let's remove lines 192-195.
Assistant
Now let's update
DefaultAiServices.javato use the newreturnType-based call and handle non-Result immediate return.Assistant
There's duplication (lines 334-335 and 368-375). Let's fix.
Assistant
Now remove duplicated lines 366-372 (the leftover
}, blank, ChatResponse..., blank, var response = invokeOutputGuardrails().Assistant
Delete lines 367-374 (keep 366 blank, then continue at 375's content becomes the real one). Let's delete lines 367 to 374 inclusive.
Assistant
I over-deleted; need
ChatResponse aggregateResponse = ...;andvar response = invokeOutputGuardrails(lines back.Assistant
Good, this looks correct now. Let's check the full block from line 330 to 402 once more for sanity, and check for duplicate
verifyModerationIfNeeded.Assistant
Good, looks correct. Now let's compile.
Assistant
Unrelated revapi failure not from our code. Let's skip that check.
Assistant
Maven resolves all dependencies upfront regardless. Let's build these test-scope dependency modules too.
Assistant
That succeeded. Now let's try building langchain4j module.
Assistant
Good compile progress. Need to fix deprecated constructor to set immediateReturnValue = null.
Assistant
Compiles successfully now. Let's run the relevant tests.
Assistant
Network access seems available. Let's run more broadly.
Assistant
All passed. Now let's run the entire tool test package plus broader test suite for AiServices to check nothing else broke.
Assistant
Passed silently (no errors). Now let's write a quick reproduction test using a temp script to validate the new feature behavior.
Assistant
Confirmed. Now let's fix the
toTextmethod for String return type null case, and remove the temp test files.Assistant
Extra
}on line 199. Let's remove it.Assistant
Now let's re-add the test file and run it again.
Assistant
All tests pass. Now let's also check the "type mismatch" case and primitive return type case for completeness, and run the full ToolService/DefaultAiServices related test suites plus broader regression tests.
Assistant
No errors. Let's run the entire langchain4j test suite to be thorough (may take a while).
Assistant
All tests in the
langchain4jmodule pass. Let's clean up temp test file since we shouldn't add new tests per instructions (but this is a reproduction script — instructions say not to add new tests to repo). Let's remove it before submitting.Assistant
Let's also fix the two "Only allowed on AI services returning Result" lines below since they're now outdated.
Assistant
Now let's rebuild core module and langchain4j module to make sure everything still compiles.
Assistant
Now let's re-run the full test suite once more for langchain4j module (and core) to confirm everything remains green after the javadoc-only core change.
Assistant
All tests pass. Let's also run core module tests (langchain4j-core) to be safe since we edited javadoc there (should compile fine, but let's verify tests there too).
Assistant
Exit=0, these are expected test-logged warnings (test intentionally throws and catches exceptions), not failures. Good, core tests pass too.
Assistant
Let's review the full diff once more for correctness and cleanliness.
Assistant
This looks good. Now let's also check
AiServiceStreamingResponseHandler.javasince it referenced ReturnBehavior — verify it's unaffected (streaming path uses a different mechanism, notexecuteInferenceAndToolsLoop).Assistant
Good, matches. Now let's do a final full diff review and clean up any leftover temp files.
Assistant
Everything looks clean, no leftover test files, and only the intended source files are modified. Let's do one more full verification build + test run to be safe before submitting.
Assistant
All builds and tests pass successfully. Let's review the checklist items now.
Checklist review:
Assistant
-
If an error was reported in the issue description, I have successfully reproduced it. — Yes. This was a feature request rather than a reported bug, but I reproduced the described limitation: I wrote a throwaway test confirming that before my fix, an AI Service method with and a non- return type threw , and that a tool returning for a non- return type crashed with (a related pre-existing bug that blocked the "null result" case explicitly required by the issue).
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.