Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-opus-5· anthropicHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION [FEATURE] support non Result return types for AiService when tool…

  1. 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 void or all return null or 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 using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to 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.
    • I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.

    Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!

    (Current directory: /langchain4j, current file: none) bash-$

  2. Claude Assistant

    Now let me make the edits to ToolService.

  3. Claude Assistant

    Now add imports and the PRIMITIVE_TO_BOXED_TYPE map.

  4. Claude Assistant

    Now add the PRIMITIVE_TO_BOXED_TYPE constant.

  5. Claude Assistant

    Now update ToolServiceResult to hold immediateReturnValue.

  6. Claude Assistant

    Now update DefaultAiServices.

  7. Claude Assistant

    Now let's compile.

  8. Claude Assistant

    Now run the broader existing test suites in the langchain4j module related to services/tools.

  9. Claude Assistant

    Reproduced the original failure. Now restore the fix and confirm it passes.

  10. Claude Assistant

    Implementation complete. Summary of changes:

    • ToolService.executeInferenceAndToolsLoop(...): now takes the AI Service method's Type returnType (instead of boolean isReturnTypeResult) and no longer unconditionally throws for non-Result return types. It collects the current round's ToolExecutions and computes an immediate return value.
    • New ToolService.immediateReturnValue(returnType, returnBehaviors, toolExecutions) implementing the requested rules in order: Result → null (unchanged behavior), void/Void → null, any TO_LLM in the immediate round → IllegalConfigurationException, all tool results null → null (or exception if the return type is primitive), exactly one non-null result assignable (with boxing) to the return type → that object, otherwise IllegalConfigurationException.
    • ToolServiceResult: new immediateReturnValue field/accessor/builder method (@Internal class).
    • DefaultAiServices: on an immediate tool return, returns the Result as before for Result<T> methods, otherwise returns the resolved immediateReturnValue (firing the completion event as usual).
    • Docs/Javadoc (tools.md, ReturnBehavior) updated to describe the new supported return types and rules; langchain4j/revapi.json got a justified ignore for the internal signature change.

    Checklist:

    • "If an error was reported in the issue description, I have successfully reproduced it." — Done. I wrote a temporary test (ReproTest) with an AI Service returning String and an IMMEDIATE tool and ran it against the stashed (original) code: it failed with IllegalConfigurationException: AI Service method must return a dev.langchain4j.service.Result type to use tools with ReturnBehavior.IMMEDIATE/IMMEDIATE_IF_LAST.
    • "I have confirmed that my fix resolves the error." — Done. After restoring my changes the same test passes (returns "immediate"), and a broader temporary test class (14 cases: String, void, Integer, int, POJO, multiple results, TO_LLM mix, Result<T>, IMMEDIATE_IF_LAST, no-immediate-return) all passed. Both temporary test files were deleted before submitting.
    • "I have thought about edge cases." — Covered: void/Void methods, primitive return types with all-null tool results (exception) vs boxed types (null), primitive boxing for compatible results (int ← Integer), multiple non-null results (exception), incompatible types (exception), TO_LLM mixed with IMMEDIATE_IF_LAST (exception, but only for the round that triggers immediate return, so multi-round flows still work), Result<T> behavior left untouched, streaming path unaffected, null returnType guarded.
    • "I have run existing tests that might have been affected." — Done. Full langchain4j module test suite (1385 tests) and langchain4j-core pass, including ReturnBehaviorCombinationsTest, ProgrammaticCreatedImmediateReturnToolTest, StreamingProgrammaticCreatedImmediateReturnToolTest, ToolServiceTest. I also verified mvn verify (revapi API check) passes for both modules; the existing IT assertion should_throw_using_immediate_tool_on_service_not_returning_Result still matches because the new mismatch message contains both IMMEDIATE and the Result class name. No existing tests were modified.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.