ai icon indicating copy to clipboard operation
ai copied to clipboard

Tool execution does not respect unsafe `finishReason` values in `streamText()` and `generateText()`

Open canblmz1 opened this issue 3 months ago • 8 comments

Description

Valid tool calls are auto-executed even when the enclosing model call terminates with length, error, content-filter, or other. I reproduced this on current main (54686d6, ai 7.0.68, @ai-sdk/openai 4.0.43).

Malformed tool-call JSON is already rejected correctly (parseToolCall marks it invalid and it never executes), so this is not an argument-parsing issue — it's specifically about execution gating based on the terminal model state.

Current behavior

In streamText(), executeToolsFromStream() queues tool calls on case 'tool-call': and releases the whole queue via Promise.all(...) on case 'model-call-end': (packages/ai/src/generate-text/execute-tools-from-stream.ts). The value of finishReason is never checked before dispatch — only whether the call is valid, has an execute function, and is approved.

In generateText(), the same gap exists even though the full response — including finishReason — is already known synchronously before executeTools() runs (packages/ai/src/generate-text/generate-text.ts), so this isn't a streaming/ordering issue either. The filter there is !toolCall.invalid && !blockedToolCallIds.has(toolCall.toolCallId) — no finishReason check.

Representative results (provider-independent, using the package's own mock model / test stream utilities):

  • finishReason: 'stop' → executes
  • finishReason: 'length' → executes
  • finishReason: 'error' → executes
  • finishReason: 'content-filter' → executes
  • finishReason: 'other' → executes
  • malformed tool-call JSON → correctly does not execute

Separately, a real @ai-sdk/openai Responses adapter test showed that a raw transport close with no response.completed and no [DONE] still results in the adapter synthesizing a terminal finish with finishReason: 'other' (packages/openai/src/responses/openai-responses-language-model.ts, unconditional flush()), which then reaches the same unguarded execution path.

Why this matters

For side-effecting tools (filesystem, shell, network, or other external-service mutations), a truncated, failed, filtered, or otherwise unconfirmed model response can still cause the tool to execute.

Expected behavior

I'd expect auto-execution to either gate on an explicitly safe terminal state, or expose an opt-in policy/hook letting callers decide which finish reasons are safe to execute under. There's already a manual workaround today — defining tools without execute and dispatching them after inspecting the terminal result — but the standard auto-execution path doesn't provide this safety by default.

Note: this is not the older "execute before finish is known" streaming-ordering issue — current streamText() already queues execution until model-call-end arrives. The remaining gap is that the value of that terminal finishReason is never used as an execution gate, and the identical gap exists in generateText() with no streaming/ordering involved at all.

Reproduction

Minimal conceptual reproduction using the package's own provider-independent test utilities (@ai-sdk/provider-utils/test, MockLanguageModelV4):

  1. Mock model returns one valid tool-call stream part, then a terminal part with finishReason: 'length'.
  2. Observe the tool's execute() still runs once.
  3. Same result reproduces for error, content-filter, and other.
  4. A genuinely malformed JSON tool-call input, by contrast, is correctly marked invalid and does not execute.
  5. The same finishReason-independent execution occurs in generateText() when doGenerate() returns a complete response with a non-stop finishReason and a valid tool call.

Happy to provide a minimal regression test / PR against the existing test harness if useful.

AI SDK Version

  • ai: 7.0.68 (also reproduced at 7.0.66)
  • @ai-sdk/openai: 4.0.43 (also reproduced at 4.0.42)

Code of Conduct

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

canblmz1 avatar Aug 19 '26 08:08 canblmz1

Identify issue type

Identified as Bug
Confidence: High

Rationale

The issue reports a concrete safety-related failure in existing automatic tool execution: terminal failure states are known but ignored before dispatching tools.

Evidence

  • Valid tool calls are auto-executed after length, error, content-filter, and other finish reasons.
  • The behavior reproduces in both streamText() and generateText() using provider-independent mock utilities.
  • Malformed tool calls are correctly rejected, isolating the failure to missing finish-reason execution gating.
  • This can trigger side-effecting tools after a truncated, failed, or filtered model response.

ai-sdk-factory[bot] avatar Aug 19 '26 08:08 ai-sdk-factory[bot]

Bug Reproduction - AI SDK 7

Bug was reproduced.

Branch: main PR: successful reproduction pull request

Details

  • The checkout matches the reported setup: commit 54686d6, ai 7.0.68, and @ai-sdk/openai 4.0.43.
  • Both generateText() and streamText() executed the valid side-effecting tool for every unsafe finish reason tested: length, error, content-filter, and other; the expected safe behavior is zero automatic executions.
  • Control cases passed: valid calls ending with stop executed once, while malformed JSON calls were marked invalid and never executed, isolating the defect to finish-reason gating.
  • Runnable reproduction added at examples/ai-functions/src/reproduction/issue-19063-unsafe-finish-reason-tool-execution.ts.

ai-sdk-factory[bot] avatar Aug 19 '26 08:08 ai-sdk-factory[bot]

Bugfix - AI SDK 7

Created bugfix and opened PR: #19066

Root Cause

Both generateText() and executeToolsFromStream() dispatched valid, approved tool calls without checking the already-known terminal finish reason; the reproduction confirmed execution for length, error, content-filter, and other.

Summary

Added a shared finish-reason allowlist so automatic tools execute only after stop or tool-calls, applied it to generated and streamed responses, removed reproduction-only artifacts, and added a patch changeset.

Testing

Added regression tests covering both generateText() and streamed tool execution for all four unsafe finish reasons.

ai-sdk-factory[bot] avatar Aug 19 '26 08:08 ai-sdk-factory[bot]

Bug Reproduction - AI SDK 5

Bug was reproduced.

Branch: release-v5.0 PR: successful reproduction pull request

Details

  • On release-v5.0 (ai 5.0.240), both generateText() and streamText() executed the valid side-effecting tool once for every unsafe finish reason tested: length, error, content-filter, and other; expected execution count was zero.
  • Control cases passed: valid calls ending in stop executed once, while malformed tool-call JSON was marked invalid and never executed.
  • generateText() executes parsed tool calls without checking the completed response's finish reason; the v5 streaming implementation in packages/ai/src/generate-text/run-tools-transformation.ts executes upon receiving the tool call, before the terminal finish reason is processed.
  • Runnable reproduction artifacts are examples/ai-functions/src/reproduction/issue-19063-unsafe-finish-reason-tool-execution.ts and examples/ai-functions/package.json; the script fails only when unsafe terminal states cause automatic execution.

ai-sdk-factory[bot] avatar Aug 19 '26 19:08 ai-sdk-factory[bot]

Bug Reproduction - AI SDK 6

Bug was reproduced.

Branch: release-v6.0 PR: successful reproduction pull request

Details

  • On release-v6.0 with ai 6.0.259, both generateText() and streamText() executed the valid tool once for each unsafe finish reason: length, error, content-filter, and other; expected execution count was zero.
  • Control cases passed: valid calls ending with stop executed once, while malformed tool-call JSON was marked invalid and never executed.
  • The provider-independent reproduction confirms that side-effecting tools can run despite an unsafe terminal model state.
  • Runnable reproduction: examples/ai-functions/src/reproduction/issue-19063-unsafe-finish-reason-tool-execution.ts.

ai-sdk-factory[bot] avatar Aug 19 '26 19:08 ai-sdk-factory[bot]

Bugfix - AI SDK 6

Created bugfix and opened PR: #19120

Root Cause

generateText dispatched valid tool calls without checking the known finish reason, while streamText began execution before evaluating the terminal finish reason. The reproduction confirmed execution for length, error, content-filter, and other.

Summary

Added a shared safe-finish-reason allowlist, gated generateText execution, and queued streamed tool calls until a stop or tool-calls finish reason is received. Added a patch changeset and removed reproduction artifacts.

Testing

Added parameterized generateText and streaming regressions for all four unsafe finish reasons and updated abort-stream coverage for deferred execution ordering.

ai-sdk-factory[bot] avatar Aug 19 '26 21:08 ai-sdk-factory[bot]

Bugfix - AI SDK 5

Created bugfix and opened PR: #19121

Root Cause

generateText dispatched parsed tool calls without checking the completed response finish reason, while streamText executed calls before receiving the terminal reason. The reproduction confirmed execution for length, error, content-filter, and other in both APIs.

Summary

Added a shared finish-reason allowlist permitting automatic execution only for stop and tool-calls. Streaming tool calls are now queued until the terminal reason is known, and reproduction-only artifacts were removed.

Testing

Added parameterized generateText and streaming regression tests for all four unsafe finish reasons and updated stream-order expectations for deferred execution.

ai-sdk-factory[bot] avatar Aug 19 '26 21:08 ai-sdk-factory[bot]

:rocket: Published in:

Package Version
ai 7.0.70 github npm
@ai-sdk/alibaba 2.0.33 github npm
@ai-sdk/amazon-bedrock 5.0.59 github npm
@ai-sdk/angular 3.0.70 github npm
@ai-sdk/anthropic 4.0.40 github npm
@ai-sdk/anthropic-aws 2.0.32 github npm
@ai-sdk/assemblyai 3.0.28 github npm
@ai-sdk/azure 4.0.45 github npm
@ai-sdk/baseten 2.1.10 github npm
@ai-sdk/black-forest-labs 2.0.29 github npm
@ai-sdk/bytedance 2.0.30 github npm
@ai-sdk/cartesia 3.0.23 github npm
@ai-sdk/cerebras 3.0.32 github npm
@ai-sdk/code-mode 1.0.27 github npm
@ai-sdk/cohere 4.0.28 github npm
@ai-sdk/deepgram 3.0.28 github npm
@ai-sdk/deepinfra 3.0.32 github npm
@ai-sdk/deepseek 3.0.29 github npm
@ai-sdk/elevenlabs 3.0.29 github npm
@ai-sdk/fal 3.0.29 github npm
@ai-sdk/fireworks 3.0.35 github npm
@ai-sdk/fish-audio 3.0.6 github npm
@ai-sdk/gateway 4.0.56 github npm
@ai-sdk/gladia 3.0.28 github npm
@ai-sdk/gmicloud 3.0.3 github npm
@ai-sdk/google 4.0.47 github npm
@ai-sdk/google-vertex 5.0.58 github npm
@ai-sdk/groq 4.0.29 github npm
@ai-sdk/harness 1.0.77 github npm
@ai-sdk/harness-acp 1.0.15 github npm
@ai-sdk/harness-claude-code 1.0.80 github npm
@ai-sdk/harness-cline 1.0.5 github npm
@ai-sdk/harness-codex 1.0.79 github npm
@ai-sdk/harness-deepagents 1.0.77 github npm
@ai-sdk/harness-grok-build 1.0.14 github npm
@ai-sdk/harness-opencode 1.0.78 github npm
@ai-sdk/harness-pi 1.0.79 github npm
@ai-sdk/huggingface 2.0.32 github npm
@ai-sdk/hume 3.0.28 github npm
@ai-sdk/klingai 4.0.30 github npm
@ai-sdk/langchain 3.0.70 github npm
@ai-sdk/llamaindex 3.0.70 github npm
@ai-sdk/lmnt 3.0.28 github npm
@ai-sdk/luma 3.0.29 github npm
@ai-sdk/mcp 2.0.34 github npm
@ai-sdk/minimax 3.0.16 github npm
@ai-sdk/mistral 4.0.30 github npm
@ai-sdk/moonshotai 3.0.36 github npm
@ai-sdk/open-responses 2.0.29 github npm
@ai-sdk/openai 4.0.44 github npm
@ai-sdk/openai-compatible 3.0.32 github npm
@ai-sdk/otel 1.0.70 github npm
@ai-sdk/perplexity 4.0.30 github npm
@ai-sdk/policy-opa 1.0.70 github npm
@ai-sdk/prodia 2.0.29 github npm
@ai-sdk/provider-utils 5.0.28 github npm
@ai-sdk/quiverai 2.0.28 github npm
@ai-sdk/react 4.0.73 github npm
@ai-sdk/replicate 3.0.29 github npm
@ai-sdk/revai 3.0.28 github npm
@ai-sdk/rsc 3.0.70 github npm
@ai-sdk/sandbox-just-bash 1.0.77 github npm
@ai-sdk/sandbox-vercel 1.0.77 github npm
@ai-sdk/svelte 5.0.70 github npm
@ai-sdk/togetherai 3.0.33 github npm
@ai-sdk/tui 1.0.71 github npm
@ai-sdk/valibot 3.0.28 github npm
@ai-sdk/voyage 2.0.28 github npm
@ai-sdk/vue 4.0.70 github npm
@ai-sdk/workflow 2.0.0 github npm
@ai-sdk/workflow-harness 1.0.77 github npm
@ai-sdk/xai 4.0.41 github npm

github-actions[bot] avatar Aug 19 '26 22:08 github-actions[bot]