Stop LLM Hallucinations With Negative Validation Rules
- llm
- ai engineering
- tool-calling
- schema validation
- prompt engineering

In short
- Standard JSON schemas are often treated as loose suggestions by LLMs, leading to frequent type coercion errors.
- Moving validation constraints into the function description field ensures they remain visible within the model's active attention context.
- Negative constraints—explicitly stating what an input cannot be—are more effective than positive instructions at pruning hallucination-prone tokens.
- Using imperative language regarding system consequences helps prevent models from attempting to 'helpfully' correct schema data types.
Hardening Structured Tool-Calling Against Malformed Parameter Payloads
LLMs frequently generate malformed tool arguments because standard JSON schemas define expected types but fail to enforce semantic constraints on value ranges, lengths, or formats. By embedding "negative validation rules" directly into the tool description—rather than the schema object—you force the model to acknowledge forbidden input patterns before it commits to a payload. This approach treats the LLM's predictive engine as a constraint-satisfaction problem where the model must navigate a "no-go zone" for parameter values.
Technical Takeaways
- Standard JSON schema types act as loose suggestions, whereas negative reinforcement instructions within the function description serve as tactical constraints on the model's token selection process.
- Appending a
validation_rulessection to thedescriptionfield is necessary because many production tool-calling APIs discard custom keys in the JSON schema, rendering schema-level constraints invisible to the model. - Negative constraints—specifying exactly what an input cannot be—are uniquely effective because they prune the model's high-probability next tokens, directly countering the model's tendency to hallucinate "helpful" corrections.
- Malformed payloads occur when the model attempts to "correct" input by reformatting data to satisfy its internal training associations rather than adhering to the technical schema constraints.
Why Schema Definitions Fail Without Negative Constraints
Standard JSON schemas are declarative, but LLMs interact with them as natural language instructions, often prioritizing their internal training weights over the technical constraints defined in your code. When a prompt provides an ambiguous objective, the model frequently performs aggressive type coercion, such as passing a floating-point number into an integer field or omitting a field it deems "unnecessary" despite the required array.
Because most standard tool-calling APIs strip custom fields like negative_validation_rules from the JSON schema before passing them to the model, the "schema-centric" approach often results in a silent failure. The model never sees your custom validation keys. By moving these constraints into the description field, you ensure the instructions are processed as part of the model's active attention context.
When This Fails
- This approach fails if the total length of the function description exceeds the model's effective context window, leading to truncated instructions.
- It also fails when the model identifies an implicit "logical override" in the user's prompt that directly contradicts the negative rules; the model will often prioritize the user's intent over your safety constraints.
Implementing Negative Validation Rules
You force strict parameter compliance by appending a set of negative rules to the end of every function description provided to the LLM. By explicitly stating what an input cannot be, you narrow the search space for the model’s next token prediction and force the model to evaluate the boundary conditions of your API before it populates the structured output.

# Implementation for a standard Tool definition structure
# Note: Place constraints directly inside the 'description' string
tool_definition = {
"name": "update_database_entry",
"description": (
"Updates a user record. "
"NEGATIVE VALIDATION RULES: "
"- user_id: Must not contain spaces, must not be an integer, "
"must not be 'null'. "
"- age: Must not be a decimal, must not be a string, "
"must be >= 18."
),
"parameters": {
"type": "object",
"properties": {
"user_id": {"type": "string"},
"age": {"type": "integer"}
},
"required": ["user_id", "age"]
}
}
This structural injection ensures that if the model attempts to pass an age of "25.5," the rule "must not be a decimal" acts as a guardrail. By moving these rules into the description, you ensure they remain visible to the model's attention mechanism regardless of the API's schema parser.
Handling Hallucinated Parameters and Type Coercion
LLMs hallucinate optional parameters or modify required types because they interpret the prompt as a creative task rather than a strict programmatic contract. To secure the interface, you must treat the tool-calling environment as untrusted, enforcing boundaries that discourage the model from prioritizing perceived intent over schema invariants.
We avoid "soft" instructions like "please try to use integers." Instead, we use imperative, high-consequence language: "The system will throw an error if this parameter is not an integer. Do not attempt to cast inputs." By explicitly stating that the system will fail, you provide the model with a simulated consequence that discourages it from taking liberties with type coercion.
Edge Cases in Coercion
- The "Zero-Value" Trap: Models often attempt to supply a default
0for empty fields even when the schema requires a specific format. - Type-Drift: When parameters share names with common programming variables, the model may attempt to "help" by formatting them as code (e.g., passing a Python list instead of a JSON array).
Debugging and Iterative Refinement
The most reliable way to maintain stable tool-calling is to track failures by capturing the full request and response logs during your integration tests, allowing you to refine your negative validation rules. If a model consistently returns a float for an integer field, it indicates that the model's training data associates that specific parameter name with numeric values that are frequently decimals.

Strategies for Reinforcement
- Example Injection: Within the description, provide a single "Never" example: "Never pass a value like 25.0; use 25 instead."
- Log Analysis: If the model continues to fail, treat the tool-calling output as an unparsed string first. If you see the model consistently failing on a specific input, your rule is likely too abstract and requires a concrete "if-then" prohibition.
Next Step
Copy one of your existing tool definitions and migrate your validation logic from the schema object into the function description field, then run a batch of test inputs through your validator to monitor for improvements in type compliance.
Want the prompt this article describes, built for your own objective?
Engineer one now — free, no account needed
