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
Now let me look at DefaultAiServices around line 349 to see how the result is handled:
- Assistant
Now let me check the callers of
executeInferenceAndToolsLoopand the existing tests: - Assistant
Let me view the full DefaultAiServices imports and top part:
- Assistant
Now I have the full picture. Let me implement. First,
ToolService: - Assistant
Now
DefaultAiServices: - Assistant
Now add the resolution helper next to
fireEventAndReturn: - Assistant
Now add the needed imports:
- Assistant
Now let me check how
ServiceOutputParserhandlesvoid, since void return types become meaningful with this feature: - Assistant
Void tool methods yield a null
resultObject(). Now update theReturnBehaviorjavadoc: - Assistant
Now compile the changed modules:
- Assistant
Compiled. Now check for existing tests asserting the old exception message:
- Assistant
Now restructure
ToolService— remove the throw, expose final-round behaviors: - Assistant
Now add
finalResponseReturnBehaviorstoToolServiceResult: - Assistant
Now update
DefaultAiServices— the call site and the resolver: - Assistant
Now write the test class. First verify
ChatModelMockAPI: - Assistant
Two failures are test-design issues. Let me check how
toResultMessagehandles empty contents (a@Toolreturning null String): - Assistant
Two test-design issues: a
@ToolString method returningnullbreaks message building (pre-existing, unrelated), andObjectis not a supported AI Service return type. Adjust the tests: - Assistant
All pass. Check for other tests referencing the old behavior, then run the module's full unit test suite:
- Assistant
While that runs, update the docs. Let me look at the relevant docs section:
- Assistant
The background run will notify on completion. Meanwhile, apply the project's formatting:
- Assistant
Let me verify no other modules use the changed APIs:
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.