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
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.
- Assistant
I’ll trace the AiService invocation and immediate tool-result handling, locate relevant tests, then implement the return-type resolution rules with focused verification and existing test runs. I’ll avoid modifying tests and will inspect the diff before submission.
- Assistant
The immediate path currently only constructs
Result, whileToolServicerejects every non-Resultmethod before the caller can inspect tool values. I’ll move that policy toDefaultAiServices: preserveResultbehavior, return null for void/nullable all-null results, return one compatible non-null raw tool result, and throw configuration errors for TO_LLM mixing, primitive-null, multiple values, or incompatible values. - Assistant
Compilation now succeeds. I also noticed primitive return methods need boxed tool values (reflection returns
Integerfor anintmethod), so I’m tightening compatibility to account for Java primitive wrappers while still rejecting null primitive results. - Assistant
The diff now implements the requested decision tree and preserves completion events for all immediate returns. The module compilation and whitespace checks pass; I’ll run the core module test suite next (without changing any tests), then submit if it remains clean.
- Assistant
Verification is complete: the affected
langchain4jmodule tests pass (the output contains only expected runtime warnings), compilation passes, andgit diff --checkreports no issues. The implementation also handles boxed primitive results and rejects null primitive outcomes. I’m submitting the two source changes now.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.