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
Let me start by understanding the repository structure and the issue. The issue is about supporting non-Result return types for AiService when tool execution is IMMEDIATE. Let me explore the repository first.
Let me look at the repository structure and understand the codebase.
Assistant
Let me look at the core module structure and find relevant AiService code
Assistant
Let me look more specifically for the service that handles AiService, particularly the part where tool execution results are handled with IMMEDIATE ReturnBehavior.
Assistant
Let me search for the main AiService class and the DefaultAiServices or similar:
Assistant
Let me look at
DefaultAiServices.javawhich should contain the core implementationAssistant
Let me try again with a different approach.
Assistant
Let me open this file in chunks to understand it.
Assistant
Let me continue reading the file, particularly looking for where tool execution results are handled with IMMEDIATE ReturnBehavior.
Assistant
Let me read the key section around line 336 where tool execution results are handled:
Assistant
Now let me look at the
ToolServiceResultclass to understand the return valuesAssistant
Let me read that file.
Assistant
Now let me look at the ToolExecution class:
Assistant
Let me look at the ToolService to understand the execution flow:
Assistant
Let me find
executeInferenceAndToolsLoopin the ToolService:Assistant
Let me read that method
Assistant
Now let me look at the
TypeUtilsclass for type resolution helpers:Assistant
Let me read this file.
Assistant
Let me also look at the
Resultclass andReturnBehaviorenumAssistant
Let me look at existing tests for return behavior combinations and the relevant test files
Assistant
Let me look at the test file.
Assistant
Let me look at the AiServicesWithToolsIT and AiServicesWithToolsWithoutMemoryIT to understand the test
Assistant
Let me read the full method in DefaultAiServices around lines 300-410
Assistant
Let me look at the
Resultclass:Assistant
Let me look at the service
Resultclass:Assistant
Let me search for existing tests or references to non-Result immediate return
Assistant
Let me look at `ProgrammaticCreatedImmediateReturnToolTest.java
Assistant
Let me check the
ToolServiceContextclassAssistant
Now I have a complete understanding of the issue. Let me implement the changes.
First, let me modify
ToolService.javato not throw when!isReturnTypeResultfor all-immediate tools (no TO_LLM):Assistant
Now let me edit
ToolService.javato change the exception condition:Assistant
Let me open the file at the right line:
Assistant
Now let me apply the change to ToolService.java
Assistant
Let me try with the replace command
Assistant
Now let me modify
DefaultAiServices.javato handle the non-Result immediate return case. Let me open it at the rightAssistant
Now let me add the non-Result immediate return handling. I'll insert code after line 361:
Assistant
Now I need to add the
handleImmediateToolReturnmethod. Let me find a good place to add it near the other helper methods.Assistant
Let me add the
handleImmediateToolReturnmethod after thefireEventAndReturnmethod. Let me check what's at line 410-412Assistant
Let me check the imports in DefaultAiServices
Assistant
Now let me add the
handleImmediateToolReturnmethod. I need to add it afterfireEventAndReturnand also add the necessary imports.Assistant
Now I need to add the import for
ToolExecution. Let me check the current imports and add it.Assistant
Let me add the import for
ToolExecution.Assistant
Let me open the file at the imports to add the new
Assistant
Now let me add the import for
ToolExecution.Assistant
Now let me verify the changes compile. Let me try building.
Assistant
Let me check if the build issue is pre-existing by restoring my changes temporarily
Assistant
Let me wait for the output.
Assistant
The original code compiles fine. Let me restore my changes.
Assistant
Let me try compiling again.
Assistant
Let me check the import section.
Assistant
I see - I accidentally removed the
TokenStreamAdapterimport. Let me fix thatAssistant
Now let me try compiling again.
Assistant
Compilation succeeds. Now let me run the relevant tests to verify the changes don't break anything.
Assistant
All 62 tests pass. Let me also run the ProgrammaticCreatedImmediateReturnToolTest
Assistant
Let me also run the
AiServicesWithToolsWithoutMemoryITandLazyCalculationIntegrationTesttests:Assistant
Let me also run the broader AiServicesWithTools tests.
Assistant
Let me review the full diff of my
Assistant
Let me check if there's a streaming implementation that also needs to be modified. Let me search for the streaming
Assistant
Let me look at the streaming response handler to see if it needs similar changes.
Assistant
Let me now write a simple test to verify the new behavior. Let me look at the test model classes first.
Assistant
Let me write a temporary test to verify my implementation works correctly.
Assistant
Let me also check the streaming handler for any needed changes.
Assistant
Let me run the full test suite for the langchain4j module (excluding tests that need API keys).
Assistant
All 1385 tests pass. Let me clean up the temporary test file.
Assistant
Let me do a final review of the complete diff.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.