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
I’ll first locate the AI service invocation and immediate tool-result handling, then inspect relevant tests and conventions. After reproducing the current restriction with the narrowest applicable test suite, I’ll implement the return-resolution rules and verify edge cases and existing tests.
- Assistant
ripgrepis unavailable in this environment, so I’ll use recursivegrepto identify the implementation and test coverage. - Assistant
The restriction appears in
ToolService, while actual method-return conversion likely occurs in the AI service proxy. I’ll inspect both the validation and immediate execution data flow before changing behavior. - Assistant
The current code throws as soon as immediate return is selected for any non-
Resultmethod. I’ll traceToolServiceResult.immediateToolReturninto the invocation handler to determine the cleanest place to resolvevoid, null, and assignable object results while preservingResultbehavior. - Assistant
The raw tool object is retained in each
ToolExecutionResult, so no serialization is needed. The rules imply non-Resultresolution must inspect only executions from the terminating round (not accumulated prior round trips), reject mixedTO_LLM, reject ambiguous multiple non-null values, and validate method return compatibility (including primitive wrappers). I’ll inspect existing exception and reflection utilities plus tests asserting the old restriction. - Assistant
A minimal design is to pass the declared
TypeintoToolService, resolve the terminating batch there (where return behaviors and raw results are both available), and carry the resolved value inToolServiceResult. This avoids accidentally considering tools from earlier model round trips or losing dynamic-provider behavior metadata. I’ll check builder conventions andTypeUtilsvisibility, then implement without changing public interfaces. - Assistant
I’ll add an internal immediate value to
ToolServiceResult, replace the boolean return-type argument with the declaredType, and implement deterministic resolution inToolService. Error messages will retain guidance aboutResult(so existing diagnostics remain useful) while explaining null, ambiguity, incompatibility, orTO_LLMmixing. - Assistant
Python is not installed, so no files were changed. I’ll use the repository editor commands for the same focused modifications.
- Assistant
The constructor fields are now correct. I’ll finish the accessor/builder and include the value in object methods, then modify the service logic.
- Assistant
The line-based edits shifted after each insertion and corrupted the latter half of the file. I’ll immediately restore this file from Git and reapply the change atomically with
perlsubstitutions against exact blocks, which avoids moving line-number hazards. - Assistant
The result carrier is now clean. Next I’ll add the resolver in
ToolService, honoring the rule order (voidfirst, then mixedTO_LLM, then nullability, then exactly one assignable non-null value), with primitive-to-wrapper compatibility. - Assistant
Imports and signature were updated, but the editor’s replacement only searches the displayed window and the target was above it. I’ll navigate to the branch and apply it, then insert the resolver adjacent to
shouldReturnImmediately. - Assistant
I’ll insert the helper now. The exception helper will consistently mention both immediate behaviors and
Result, preserving the prior actionable recommendation while adding the specific cause. - Assistant
The helper insertion needs one closing brace before the following Javadoc; I’ll add it, update the caller to pass
returnType, and make the invocation handler return the resolved immediate value for non-Resultmethods. - Assistant
The data flow is complete except for the final non-
Resultshort-circuit. I’ll add it before output guardrails/parser processing, since an immediate tool result must bypass LLM output parsing just as immediateResultalready does. Then I’ll compile and format-check. - Assistant
The inserted branch is missing its closing brace due the same inclusive-line replacement behavior; I’ll correct it. Maven also requires
HOMEin this environment, so I’ll rerun withHOME=/tmpafter checking imports and compilation syntax. - Assistant
Compilation passes. Before behavior tests, I’ll run Spotless to normalize import ordering/line wrapping, inspect the diff for unintended changes, then run the focused immediate-return test classes and the module unit suite if feasible.
- Assistant
Spotless cannot locate its ratchet reference because this checkout lacks
origin/main; no source changes were made by it. I’ll create a temporary local ref namedorigin/main, run formatting, and remove the ref afterward. This affects only local Git metadata, not the submitted diff. - Assistant
Formatting succeeded and touched only the three modified files, mostly wrapping existing concatenations around changed regions. I’ll inspect the substantive diff and then run focused tests.
- Assistant
The diff is functionally limited to the intended three files; extra hunks are formatter normalization. I’ll run the return-behavior decision tests and programmatic immediate-tool tests first.
- Assistant
Focused existing tests pass, but they do not exercise non-
Resultreturns. I’ll inspect the lightweight mock-model setup used by those tests and create a temporary standalone Java/JUnit-style harness outside the repository (or under/tmp) to verify all requested branches without adding tests to the project. - Assistant
The mock model and tool APIs make a standalone harness straightforward. I’ll compile
/tmp/ImmediateReturnHarness.javaagainst built main/test classes and Maven dependencies. It will assert: assignable object return, primitive wrapper return, nullable all-null, void despite a non-null tool result, mixedTO_LLMrejection forIMMEDIATE_IF_LAST, multiple non-null rejection, and primitive-null rejection. - Assistant
The standalone harness passes every requested branch and confirms the original blanket restriction is resolved. I’ll now run the full
langchain4jmodule test suite to detect regressions, particularly old diagnostics andToolServiceResultequality expectations. - Assistant
The full module suite passes. I’ll make two small readability-only spacing fixes around the new branches/helpers, rerun formatting/check compilation, and review final status to ensure no tests or temporary files are included.
- Assistant
Formatting and compilation pass, with exactly three production files modified. I’ll perform a final diff review for correctness and summarize the verification against each checklist item before submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.