sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[DOCS] Interventions page: say that beforeToolCall handlers receive tool input before schema validation

Open sandhya-subramani opened this issue 3 weeks ago • 1 comments

Documentation Section

https://strandsagents.com/docs/user-guide/sdk/agents/interventions/ (site/src/content/docs/user-guide/sdk/agents/interventions/index.mdx and interventions.ts)

Current State

The page explains onError, but it does not say that event.toolUse.input has not been checked against the tool's input schema when beforeToolCall runs. The TypeScript examples read fields from it directly, for example const input = event.toolUse.input as Record<string, string> in the action-type examples.

In the TypeScript SDK, a tool created with tool({ inputSchema: z.object(...) }) validates its input inside the tool callback (strands-ts/src/tools/zod-tool.ts, the two this._inputSchema.parse(input) calls). Intervention handlers run earlier, on BeforeToolCallEvent, so they receive whatever the model produced.

Suggested Improvement

  • State on the interventions page that tool input is not schema-validated when beforeToolCall runs.
  • Show the safe pattern in the examples: check the shape of the input before using it, or set onError to 'deny' on handlers that read input fields.

Rationale

Adding a handler can turn a recoverable model mistake into a failed invocation. With a tool whose schema is z.object({ paths: z.array(z.string()) }) and a model that calls it with { "path": "a" }:

Setup Result
No handler Error tool result with the Zod message; the run ends with stopReason: 'endTurn'
Handler that reads input.paths.some(...), default onError agent.invoke() rejects with TypeError: Cannot read properties of undefined (reading 'some')
Same handler with onError: 'deny' Error tool result DENIED: Handler threw: ...; the run ends with stopReason: 'endTurn'

Tested with @strands-agents/sdk 1.25.0 from npm (Node.js 25.8.1, macOS) and a scripted model.

Additional Context

Handler used in the test
class PathGuard extends InterventionHandler {
  readonly name = 'path-guard'

  override beforeToolCall(event: BeforeToolCallEvent) {
    const input = event.toolUse.input as { paths: string[] }
    if (input.paths.some((p) => p.startsWith('/etc'))) {
      return InterventionActions.deny('no /etc')
    }
    return InterventionActions.proceed()
  }
}

Only the TypeScript SDK was tested. Checked against main at 46cbb8c. This issue was drafted with an AI coding assistant; the test above was run as described.

sandhya-subramani avatar Nov 14 '26 07:11 sandhya-subramani

Could I take the documentation change in this issue? I would clarify that beforeToolCall receives model-produced input before the Zod tool callback validates it, update the field-reading examples to handle unexpected shapes or use onError: 'deny', and verify the examples with a scripted model plus the repository's TypeScript/docs checks. I would keep the SDK API and runtime behavior unchanged. I'm using Codex for implementation and verification, and will wait for maintainer confirmation before starting significant work, as CONTRIBUTING requests.

austinchennn avatar Nov 14 '26 16:11 austinchennn