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
Let me explore the repository structure to understand the codebase and find the relevant files for this issue.
Assistant
I'll explore the repository structure first.
Assistant
Let me look at the ReturnBehavior class first.
Assistant
Now let me look at the AiServices.java to understand how immediate returns are handled.
Assistant
Let me look at the key part of ToolService.java where the immediate return logic is.
Assistant
Let me look at how
isReturnTypeResultis determined and how the result is used in AiServices.java.Assistant
Let me look at the existing test files for immediate return behavior.
Assistant
Let me look at the ToolExecutionResult and ToolExecution classes to understand how the actual result object is stored.
Assistant
Let me look at the DefaultAiServices more broadly to understand the context around the immediate return handling.
Assistant
Let me look at the utility methods for type resolution in DefaultAiServices.
Assistant
Let me look at the full ToolService.java imports and the method signature area.
Assistant
Now let me look at the ToolServiceResult class fully.
Assistant
Let me check the streaming response handler for immediate returns.
Assistant
Now I have a good understanding of the codebase. Let me implement the feature. First, let me add the
immediateToolReturnResultfield toToolServiceResult.java.Assistant
The edit command needs to be invoked differently. Let me use the
editcommand properly.Assistant
These special commands need to be run as shell commands in the terminal. Let me try using the
editcommand directly.Assistant
Now let me update the constructor to initialize the new field, and the deprecated constructor too.
Assistant
Now update the deprecated constructor to initialize the new field too.
Assistant
Now let me add the accessor method for
immediateToolReturnResultafterimmediateToolReturn().Assistant
Now update equals, hashCode, toString, and the Builder to include the new field.
Assistant
Now update toString and Builder to include the new field.
Assistant
Now update the Builder to add the field and setter.
Assistant
Good. Now let me modify
ToolService.java. Let me open the relevant section.Assistant
Now let me change the method signature and the immediate return logic. First, update the method signature:
Assistant
Now let me update the immediate return logic block:
Assistant
Now let me add the
resolveImmediateReturnContenthelper method aftershouldReturnImmediately:Assistant
Now let me add the necessary imports to ToolService.java.
Assistant
I see a duplicate import was created. Let me fix that and add the TypeUtils import.
Assistant
Now add the TypeUtils import:
Assistant
Now let me update
DefaultAiServices.javato passreturnTypeand handle the non-Result immediate return case.Assistant
Let me check for any other callers of
executeInferenceAndToolsLoopthat I might have missed.Assistant
Good, only one caller. Now let me compile the project to check for errors.
Assistant
Let me check what
ToolExecution.result()returns.Assistant
I see -
ToolExecution.result()returns a String, andresultObject()returns the raw object. Let me fix the helper method.Assistant
The file open is DefaultAiServices.java, but the text is in ToolService.java. Let me use the --file option.
Assistant
Now let me compile again.
Assistant
Compilation succeeds. Now let me check if there are tests that expect the old exception behavior (throwing when non-Result type with IMMEDIATE).
Assistant
Let me look at the
ImmediateToolWithPrimitiveParametersandStringAssistantdefinitions used in that test.Assistant
The compilation succeeds. Now let me create a temporary test to verify the new behavior works correctly. Let me first check the ChatModelMock API.
Assistant
Let me create a temporary test to verify the new behavior.
Assistant
Let me verify with more explicit output to confirm all tests passed.
Assistant
All 6 tests pass. Now let me run the existing relevant tests to make sure I haven't broken anything.
Assistant
All existing tests pass. Now let me update the
ReturnBehavior.javaJavadoc to reflect the new behavior, since it currently says IMMEDIATE/IMMEDIATE_IF_LAST are only allowed on Result return types.Assistant
Let me update the class-level Javadoc to reflect the new behavior.
Assistant
Now update the per-enum Javadoc for IMMEDIATE and IMMEDIATE_IF_LAST:
Assistant
The indentation is off on the IMMEDIATE Javadoc. Let me fix it.
Assistant
Now update the IMMEDIATE_IF_LAST Javadoc's last line:
Assistant
Now let me recompile both langchain4j-core and langchain4j to verify everything compiles.
Assistant
Let me check if any test files directly reference the changed method signature or
ToolServiceResultbuilder in ways that might break.Assistant
Let me run the test compilation to ensure test sources compile with the changes.
Assistant
Let me run the broader set of service/tool tests to ensure nothing is broken.
Assistant
All tests pass. Let me run the broader AiServices tests to make sure nothing is broken.
Assistant
Let me check whether the
AbstractAiServiceWithToolsITintegration test with the old exception expectation is actually run in the langchain4j module.Assistant
All tests pass. Now let me remove the temporary test file and do a final review.
Assistant
Let me review all the changes I made with a git diff.
Assistant
Let me verify the DefaultAiServices diff looks correct.
Assistant
The changes look correct. Let me do a final compile and run the key tests one more time to confirm everything is in order.
Assistant
All tests pass. Let me do a final review of the complete diff to ensure everything is clean.
Assistant
Everything looks clean. Let me verify there are no leftover temporary files.
Assistant
All changes are clean - only the 4 intended files are modified and the temp test has been removed. Let me do a final compile to be 100% sure 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.