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 first map the repository and locate
ReturnBehavior.IMMEDIATE, AiService return-type validation, and result adaptation. Then I’ll identify the relevant tests and reproduce the current limitation before implementing the conversion rules. - Assistant
ripgrepis unavailable, so I’ll use recursivegrepto locate the implementation and tests. - Assistant
The likely restriction is in
ToolService, while the eventual AiService method return conversion may live in invocation orchestration. I’ll inspect both the validation and immediate-result data flow. - Assistant
I found the runtime guard that unconditionally rejects non-
Resultreturns. Next I’ll inspectDefaultAiServicesaroundToolServiceResulthandling and the result object APIs, because the new adaptation belongs where the service proxy knows the declared returnType. - Assistant
The proxy currently has all accumulated
ToolExecutionobjects and the generic returnType, so it can apply the requested cardinality/nullability/type rules. However, onlyToolServicehas each tool’sReturnBehavior; I’ll inspect class utilities and repository history/status to choose consistent exception wording and type-resolution semantics, then locate focused test infrastructure for a reproduction. - Assistant
The raw tool object is explicitly preserved as
ToolExecution.resultObject(). I’ll now inspectTypeUtils, exception conventions, and test model stubs. I also want to verify whether output-format generation toleratesvoidand primitives before tools execute, since supportingvoidmust not fail earlier in request construction. - Assistant
An existing integration test encodes the old behavior and will necessarily become obsolete; I will not modify it per instructions. The implementation needs two coordinated changes:
- Let
ToolServicereturn immediately for non-Resultonly when noTO_LLMexecution participated; preserve an explicit configuration error for theIMMEDIATE_IF_LASTmixed case. - In
DefaultAiServices, resolve the immediate raw tool results:void→ null; all-null → null only for references; exactly one non-null assignable value → that value; otherwise throw.
Before editing, I’ll inspect current branch history/remotes for an upstream implementation or naming clues, and the executor behavior for void/null raw values.
- Let
- Assistant
There is no remote/upstream branch available. Java reflection stores primitive tool returns boxed, while the proxy can unbox them, so compatibility must treat primitive service returns as their wrappers. I’ll inspect utility dependencies (e.g. ClassUtils) and the exact ToolService method signature/call count, then patch with a small resolver in
DefaultAiServicesand aTO_LLMguard inToolService. - Assistant
I’ll preserve existing
Resultbehavior and add explicit non-Resultadaptation. Compatibility will use the declared raw class (appropriate under this project’s existing generic return validation), with primitive-to-wrapper normalization. All tool rounds will contribute to theTO_LLMcheck, not just the final batch, preventing anIMMEDIATE_IF_LASTcall from silently discarding an earlier result sent toward the LLM. - Assistant
Python is unavailable in this environment, so no files were changed. I’ll apply the same focused edits with the repository’s
edit/replacecommands instead. - Assistant
The editor’s replacement command only operates within its displayed window; only the import was applied. I’ll use exact line-range edits after displaying the relevant ranges, which avoids ambiguous replacements.
- Assistant
I have exact ranges now. I’ll insert the cross-round behavior flag, replace the old guard, and add the return resolver immediately before completion-event handling.
- Assistant
The sequential line edits shifted later ranges and exposed two mechanical issues in review:
anyToolErroredwas dropped,finalResponseduplicated, and the helper was inserted inside the completion method. I’ll repair both blocks atomically using their current exact ranges, then compile immediately. - Assistant
The code structure is repaired and
git diff --checkpasses. Maven did not start because the wrapper requiresHOME; I’ll rerun withHOME=/tmp, allowing formatting checks to report any style issues rather than skipping them. - Assistant
Compilation succeeds. To reproduce and verify the feature without adding or modifying repository tests, I’ll build a temporary standalone Java harness under
/tmp. It will use the public AiServices API, a minimalChatModel, annotated immediate tools, and service methods covering object, primitive, nullable, void, incompatible, multiple-result, and mixedTO_LLMcases. - Assistant
The public API is straightforward enough for a temporary harness. I’ll generate the module dependency classpath, compile the harness outside the repository, and run assertions against actual proxy invocations.
- Assistant
The standalone reproduction passes all requested success and failure paths, including primitive boxing and mixed
TO_LLM/IMMEDIATE_IF_LAST. I’ll now run formatting validation and focused unit tests (ToolServiceTest, return-behavior combinations, and programmatic immediate tools), then inspect the final diff for accidental or unnecessary changes. - Assistant
Spotless could not run because this uploaded repository has no
origin/mainref; the tests were consequently not started due command chaining. I’ll create the missing local tracking ref at the current base commit (metadata only, not a submitted source change), rerun Spotless, and run the focused tests separately. - Assistant
The focused tests completed successfully (the second Maven command returned cleanly). Spotless’s ratchet formats an entire touched file, and it reports some pre-existing formatting drift in
ToolServicein addition to my new lines. Because CI will enforce the same rule once the file is touched, I’ll runspotless:apply, inspect the resulting diff size, and retain only formatter-required changes. - Assistant
Formatting adds only modest drift (mostly import ordering and wrapping), not broad unrelated rewrites. I’ll review the complete diff now, especially exception semantics and whether any formatter-only edits can safely be avoided while still passing the ratchet.
- Assistant
The diff matches the intended behavior. The only unrelated hunks are formatter-mandated changes in the two now-touched files. I’ll rerun Spotless to confirm a clean state, rerun focused tests after formatting, and run adjacent AiServices tool-error tests to guard the normal reprocessing path.
- Assistant
Focused and adjacent tests pass, and formatting is clean. I noticed the public
ReturnBehaviorJavadoc still states that immediate behaviors are only allowed forResultmethods, which would contradict the new API. I’ll inspect that section and update only the obsolete contract language if needed. - Assistant
The Javadoc explicitly prohibits the new feature in three places. I’ll update the class-level contract to document direct non-
Resultresolution and the mixed-TO_LLMrestriction, then point both enum constants to that contract. - Assistant
I’ll make the Javadoc describe both return modes without overpromising conversion: raw result objects must already be assignable to the service return type, and mixed
TO_LLMimmediate returns still requireResult. - Assistant
One constant-level sentence still says results are always “inside Result.” I’ll remove that remaining contradiction, then perform a final compile/test/harness cycle and status review.
- Assistant
Formatting, compilation, and focused tests all passed; only the final harness launch had a typo (
ImmediateHarness.javainstead of the class name). I’ll rerun it correctly, then verify status, whitespace, and the final diff summary.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.