embabel-agent icon indicating copy to clipboard operation
embabel-agent copied to clipboard

Inconsistency Between LlmTool Parameter required Definition and Execution Logic

Open zhuyouxiang opened this issue 10 months ago • 0 comments

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 because isOptional is false. This prevents Java developers from defining optional parameters for LLM tools.
  • Expected Behavior: The execution logic should prioritize the @Param annotation. If required = false, the framework should allow the parameter to be omitted or passed as null (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

  1. 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)
  1. Trigger the tool via an LLM call without providing the opt argument.
  2. invokeMethod will check param.isOptional, find it to be false, and fail to handle the missing argument correctly, likely resulting in a callBy exception.
Image

zhuyouxiang avatar Jan 21 '26 12:01 zhuyouxiang