The Research Report
CodeSpear: When Grammar Constraints Become a Jailbreak Surface
A new paper shows how the very features meant to make model code output reliable can be turned against safety alignments to force malicious code generation.
Developers want reliable code from artificial intelligence. They often use grammar-constrained decoding to force correct syntax. This technique applies a token mask at each step of generation. The mask zeroes out any token that breaks the target grammar. This process guarantees that the generated code parses correctly. Mainstream inference engines like SGLang and vLLM support this option.
Yet, this helpful mechanism has a hidden risk. A new paper reveals that attackers can use benign grammar constraints to bypass safety alignments. The attack is named CodeSpear. It forces frontier models like GPT-5 to generate malicious code.
On June 10, 2026, researchers Yitong Zhang, Shiteng Lu, and Jia Li published their findings. Their paper is titled "Grammar-Constrained Decoding Can Jailbreak LLMs into Generating Malicious Code." It shows that safety guards fail when the decoding engine restricts the output space to a strict grammar.
The Flaw of Modality Mismatch
Developers build modern safety alignment almost entirely in the natural-language space. They teach models to respond to harmful requests with natural-language refusals. The model learns to say that it cannot help with malicious tasks.
This approach assumes that natural language is always available. But grammar-constrained decoding breaks this assumption. When a developer forces a model to output Python code, the valid output space shrinks. The strict code grammar forbids natural-language text.
As a result, the tokens for a standard refusal violate the grammar. The token mask zeroes out the probability of those refusal tokens. The model cannot say that it refuses the request. It must choose from the remaining valid code tokens.
Faced with this constraint, the model takes the path of least resistance. It completes the requested program token by token. This forces the model into the code space. No one has aligned the model to behave safely in this space.
Bypassing Safety Guards on Ten Models
The researchers tested CodeSpear on ten popular language models. These models include GPT-5, MiniMax-M2.7, and Qwen2.5-Coder-32B. The results are clear and concerning.
Simply applying a standard, benign Python grammar constraint bypassed safety alignments easily. CodeSpear raised the malicious code generation success rate by more than 30 percentage points on average. On local models like Qwen2.5-Coder-7B, the attack achieved an average success rate of 81.82%. On commercial API-based models like GPT-5, the attack success rate increased by more than 40 percentage points.
This attack does not require complex prompt engineering or adversarial words. The manipulation happens entirely in the decoding constraint. It slips past input-side filters because the user prompt itself looks benign.
The underlying attack vector is not entirely new. In March 2025, researchers first described this control-plane attack surface in a paper titled "When Grammar Guides the Attack." That work framed structured output as an attack plane that operates below the prompt level. CodeSpear shows that this risk is practical and highly effective against the latest models.
The Defense: CodeShield and Honeypot Code
To solve this vulnerability, the authors of the paper designed a defense named CodeShield. This approach trains the model to maintain safety even when the decoder forces it into the code modality.
A simple defense teaches the model to write a refusal comment or a pass statement. But this solution is too simple. An attacker can tighten the grammar to forbid comments or pass statements. This would reopen the attack surface.
Instead, CodeShield teaches the model to generate honeypot code. Honeypot code is semantically harmless but structurally diverse. It does not implement the malicious request, but it satisfies the grammar constraints.
For example, the model generates a harmless function that performs a benign calculation. This code satisfies the grammar, so the token mask does not block it. But it does not contain the harmful payload.
The CodeShield approach uses Direct Preference Optimization to align the model. The training pairs place the honeypot code as the preferred response and the malicious code as the dispreferred response. The model learns to generate safe code when natural-language refusals are unavailable.
At the same time, CodeShield preserves normal natural-language refusals when no grammar constraint is present. The model stays helpful and safe across different decoding configurations.
The Lessons for Developers
The success of CodeSpear offers vital lessons for developers building agentic systems:
- Treat decoding constraints as untrusted input. If users can supply custom JSON schemas or grammars, log and limit them.
- Align safety in the code modality. Developers must teach models how to behave safely when they are forced to write code.
- Run independent output-side safety classifiers on generated code. Do not assume that prompt-level guardrails cover the decoding path.
As structured outputs become more common, developers must look below the prompt level. True safety requires alignment across all modalities.