Inconsistency Between LlmTool Parameter required Definition and Execution Logic
Bug Report: Inconsistency Between Parameter required Definition and Execution Logic
1. Description
There is a logic mismatch between the Definition Phase (createDefinition) and the Execution Phase (invokeMethod) regarding how parameter optionality is handled.
- Definition Phase: Respects the
@Param(required = ...)annotation. - Execution Phase: Relies strictly on Kotlin's reflection property
KParameter.isOptional.
This creates a conflict: If a developer marks a parameter as @Param(required = false), but the underlying Kotlin function does not provide a default value (or the method is written in Java), invokeMethod fails to treat the parameter as optional during the actual call.
2. Key Issues
A. Lack of Java Compatibility
Java does not support native "default parameter values." Consequently, for any method defined in Java, KParameter.isOptional will **always return false**.
- Current Behavior: Even if a user explicitly tags a Java parameter with
@Param(required = false), the framework ignores this during execution becauseisOptionalisfalse. This prevents Java developers from defining optional parameters for LLM tools. - Expected Behavior: The execution logic should prioritize the
@Paramannotation. Ifrequired = false, the framework should allow the parameter to be omitted or passed asnull(for non-primitive types).
B. Logic Disconnection
As seen in the current implementation:
// createDefinition uses annotation.required (Correct)
required = paramAnnotation?.required ?: !param.isOptional
// invokeMethod only checks param.isOptional (Inconsistent)
else if (!param.isOptional) { ... }
If the LLM follows the generated Schema and omits an optional parameter, invokeMethod will still treat it as mandatory because isOptional remains false. This leads to a runtime failure that contradicts the tool's own definition.
3. Steps to Reproduce
- Define a tool method in Java:
Reference File spring-ai-agent-utils AskUserQuestionTool)
Due to the lack of a proper compatibility layer, I have migrated the annotations from Spring AI to Embabel Agent
@LlmTool(name = "AskUserQuestionTool",
description = """
Use this tool when you need to ask the user questions during execution. This allows you to:
1. Gather user preferences or requirements
2. Clarify ambiguous instructions
3. Get decisions on implementation choices as you work
4. Offer choices to the user about what direction to take.
Usage notes:
- Users will always be able to select "Other" to provide custom text input
- Use multiSelect: true to allow multiple answers to be selected for a question
- If you recommend a specific option, make that the first option in the list and add "(Recommended)" at the end of the label
""")
public String askUserQuestion(
@LlmTool.Param(description = "Questions to ask the user (1-4 questions)") List<Question> questions,
@LlmTool.Param(description = "User answers collected by the permission component",
required = false) Map<String, String> answers)
- Trigger the tool via an LLM call without providing the
optargument. invokeMethodwill checkparam.isOptional, find it to befalse, and fail to handle the missing argument correctly, likely resulting in acallByexception.