Skip to content

BUG validate_temperature and validate_top_p accept NaN #3605

Closed

Description

Describe the bug

validate_temperature and validate_top_p in pyrit/prompt_target/common/utils.py accept float("nan"). Both use value < low or value > high, and every comparison with NaN is False, so NaN passes. The neighbouring validate_presence_penalty and validate_frequency_penalty use not low <= value <= high and already reject NaN, and their tests cover it.

Every target that calls these validators accepts a NaN temperature or top_p: OpenAIChatTarget, OpenAIResponseTarget, OpenAICompletionTarget, LiteLLMChatTarget and AzureMLChatTarget. The NaN is stored in the target's identifier. Each send then fails when httpx encodes the request body, with ValueError: Out of range float values are not JSON compliant, instead of the target rejecting the value up front with the usual range message.

Steps to reproduce

from pyrit.prompt_target.common.utils import validate_temperature, validate_top_p

validate_temperature(float("nan"))  # no error
validate_top_p(float("nan"))        # no error
from pyrit.prompt_target import OpenAIChatTarget

target = OpenAIChatTarget(
    endpoint="https://example.invalid/v1",
    api_key="x",
    model_name="gpt-4o",
    temperature=float("nan"),
    top_p=float("nan"),
)
print({k: v for k, v in target.get_identifier().params.items() if k in ("temperature", "top_p")})
# {'temperature': nan, 'top_p': nan}

inf and -inf are already rejected, because they compare outside the range.

Expected behavior

validate_temperature(float("nan")) and validate_top_p(float("nan")) raise PyritException with the same range messages they use for other out-of-range values, matching the penalty validators.

Proposed fix

Write both checks as not 0 <= temperature <= 2 and not 0 <= top_p <= 1, like the penalty validators, and add nan, inf and -inf cases to tests/unit/prompt_target/test_target_utils.py.

Reproduced on main dd2895d. I'd be happy to open a PR.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions