microsoft/semantic-kernel · warning · ValueError

Radius and height must be non-negative.

Error message

Radius and height must be non-negative.

What it means

A domain-validation ValueError in the sample MathPlugin.cylinder_volume: it refuses negative radius or height because a cylinder's geometric dimensions are non-negative. It is kernel_function sample logic demonstrating annotated plugin functions, not a library-level error.

Source

Thrown at python/samples/concepts/auto_function_calling/nexus_raven.py:235

    def get_prompt_execution_settings_class(self) -> type[PromptExecutionSettings]:
        return NexusRavenPromptExecutionSettings


##########################################################
# Step 1: Define the functions you want to articulate. ###
##########################################################


class MathPlugin:
    @kernel_function
    def cylinder_volume(
        self,
        radius: Annotated[float, "The radius of the base of the cylinder."],
        height: Annotated[float, "The height of the cylinder."],
    ):
        """Calculate the volume of a cylinder."""
        if radius < 0 or height < 0:
            raise ValueError("Radius and height must be non-negative.")

        return math.pi * (radius**2) * height

    @kernel_function
    def add(
        self,
        input: Annotated[float, "the first number to add"],
        amount: Annotated[float, "the second number to add"],
    ) -> Annotated[float, "the output is a number"]:
        """Returns the Addition result of the values provided."""
        return MathPlugin.calculator(input, amount, "add")

    @kernel_function
    def subtract(
        self,
        input: Annotated[float, "the first number"],
        amount: Annotated[float, "the number to subtract"],
    ) -> float:

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Pass non-negative radius and height.
  2. Take the absolute value before calling if sign is irrelevant to your use case.
  3. Improve the function description / parameter annotations so the model does not emit negatives.

Example fix

# before
if radius < 0 or height < 0:
    raise ValueError("Radius and height must be non-negative.")
return math.pi * (radius**2) * height
# after - normalize and compute
radius, height = abs(radius), abs(height)
return math.pi * (radius**2) * height
Defensive patterns

Strategy: validation

Validate before calling

def cylinder_volume(radius: float, height: float) -> float:
    if radius < 0 or height < 0:
        raise ValueError("Radius and height must be non-negative.")
    import math
    return math.pi * (radius**2) * height

# pre-call guard
def safe_cylinder_volume(radius, height):
    return cylinder_volume(abs(radius), abs(height))

Type guard

def is_non_negative(x: object) -> bool:
    return isinstance(x, (int, float)) and x >= 0

Try / catch

try:
    vol = cylinder_volume(r, h)
except ValueError as e:
    # tell the caller/agent the constraint
    print(e)

Prevention

When it happens

Trigger: Invoking the cylinder_volume kernel function (directly or via the NexusRaven auto-function-calling loop) with radius < 0 or height < 0.

Common situations: The model hallucinates a negative value, a caller passes a sign error, or a test feeds -1 to confirm validation. mathematically pi * r^2 * h would still compute for negatives, so this guard exists to enforce the physical constraint.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/79189402d1ab0693. Report an issue: GitHub.