Untitled conversation
- User
[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.
- Assistant
Let me look at the referenced Quarkus PR to align semantics:
- Assistant
Now let me check if there's an existing primitive→wrapper utility, and check the streaming handler's immediate handling briefly:
- Assistant
Now I'll implement. First,
ToolService.java: - Assistant
Now the main change — the signature and the immediate-return branch:
- Assistant
Now add the resolution helper right after
shouldReturnImmediately: - Assistant
Now
DefaultAiServices.java: - Assistant
Check that
AiServiceCompletedEventtolerates a null result: - Assistant
Test-scoped sibling artifacts aren't in the local repo. Let me build them online:
- Assistant
Revapi flags the (internal) signature change. Let me see how this repo handles revapi exceptions:
- Assistant
Now let me check whether
voidreturn type is currently supported by AiServices validation: - Assistant
Compiles. Now write tests:
- Assistant
All 13 tests pass. Now run the existing related tests to check for regressions:
- Assistant
All green. Now update the documentation:
- Assistant
Check
ReturnBehaviorjavadoc and other places mentioning theResultrequirement: - Assistant
Now rebuild and run the broader service/tool test suite:
- Assistant
Confirmed pre-existing/environmental. Final verification build with revapi enabled:
- Assistant
Done. The feature is implemented and verified. Summary of the changes:
Implementation
langchain4j/.../service/tool/ToolService.java
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.