ai icon indicating copy to clipboard operation
ai copied to clipboard

AI SDK 6 - Support "blocked" state in needsApproval for per-argument policy decisions

Open zby opened this issue 11 months ago • 6 comments

Description

Problem

needsApproval supports per-argument logic via the function signature:

needsApproval?: boolean | ((args, context) => boolean | Promise<boolean>)

This works well for two cases:

  • false → pre-approved, execute without prompting
  • true → needs user approval before execution

However, there's a third case that can't be expressed: "blocked by policy" — when the tool determines, based on the arguments, that the operation is not permitted regardless of user consent.

Example: Zone-based filesystem permissions

const writeFileTool = tool({
  parameters: z.object({ path: z.string(), content: z.string() }),
  needsApproval: async (args) => {
    const zone = resolveZone(args.path);

    if (zone.mode === 'ro') {
      // ❌ Can't express "blocked" - only true/false available
      // Returning true would prompt user, but their approval wouldn't matter
      // Returning false means "pre-approved", which is wrong
      return ???;
    }

    if (zone.approval === 'preApproved') return false;
    return true; // ask user
  },
  execute: async (args) => { /* ... */ }
});

Current workarounds

  1. Throw from needsApproval — Works but unexpected; function signature suggests it returns boolean
  2. Return true, auto-deny in approval handler — Misleading if user sees a prompt they can't actually approve
  3. Return false, throw in execute — Semantic mismatch; false means "approved"

Proposal

Extend the return type to support a third state:


  // Option A: Simple string literal
  type NeedsApprovalResult = boolean | 'blocked';

  // Option B: Object with reason (better error messages)
  type NeedsApprovalResult =
    | boolean
    | { blocked: true; reason?: string };

  needsApproval?:
    | NeedsApprovalResult
    | ((args, context) => NeedsApprovalResult | Promise<NeedsApprovalResult>)

Why this differs from not registering the tool

If approval were purely per-tool, you could just not register blocked tools. But with per-argument logic:

  • The tool is registered (valid for some arguments)
  • Certain argument combinations are blocked by policy
  • Other combinations need approval or are pre-approved

Semantics

Return value Meaning User prompted? Executes?
false Pre-approved No Yes
true Needs consent Yes If approved
'blocked' Policy violation No No (error returned to LLM)

AI SDK Version

[email protected]

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

zby avatar Dec 06 '25 20:12 zby

+1 on a third state. The important distinction is needs human consent vs cannot execute under policy.

Prompting a user for something that policy will never allow creates a confusing UI and trains users to approve meaningless prompts. I would model it as a decision enum rather than overloading boolean:

  • allow: execute
  • ask: surface approval UI
  • block: return a tool result with reason and do not execute

This also composes better with per-argument policies like path zones, spend limits, recipients, or production flags.

oleg-bk avatar May 27 '26 13:05 oleg-bk

@oleg-bk @zby did you take a look at our latest release https://www.npmjs.com/package/@ai-sdk/policy-opa / https://ai-sdk.dev/docs/agents/policy-tool-approvals

That should cover most of what you need. Let me know if you have other suggestions/feedback

dnukumamras avatar Jul 01 '26 04:07 dnukumamras

dnukumamras Thanks, this is definitely closer to the shape I meant.

The allow / deny / requires-approval split solves the main UX issue: policy-denied actions should not look user-approvable.

The remaining question for me is what happens after deny: can the system guide the agent into a safer permitted path, rather than just returning a reason?

Also glad to see transitive enforcement called out for bash / MCP / HTTP-style dispatcher tools. That’s where a lot of policy bypasses tend to appear.

oleg-bk avatar Jul 01 '26 11:07 oleg-bk

Identify issue type

{
  "evidence": [
    "The issue requests a third `needsApproval` return state such as `'blocked'` or `{ blocked: true; reason }` for policy-denied tool calls.",
    "The description frames current boolean behavior as unable to express a new policy decision state, with workarounds described as semantically poor rather than a documented behavior failure.",
    "A commenter agrees and suggests an allow/ask/block decision enum for per-argument policies.",
    "A maintainer points to a new `@ai-sdk/policy-opa` release and policy tool approvals docs as covering most needs."
  ],
  "rationale": "The issue asks for an API capability/behavior expansion to represent policy-denied tool calls distinctly from approval-required or pre-approved calls. It is not reporting broken existing behavior against current documented semantics.",
  "type": "feature",
  "confidence": "high"
}

lgrammel avatar Jul 02 '26 08:07 lgrammel

With AI SDK 7, you can have automatic allow/deny which may solve this.

lgrammel avatar Jul 15 '26 13:07 lgrammel

Feature Analysis

Feature is available in AI SDK 7.0.0-beta.113

The complete capability is available through AI SDK 7's toolApproval replacement for deprecated needsApproval: per-argument callbacks can return { type: 'denied', reason }, and passing focused tests confirm that denial requires no user response, skips execution, and returns an execution-denied result to the model.

Documentation

Related PRs

lgrammel avatar Jul 15 '26 13:07 lgrammel