Skip to content

fix: prevent xAI/Grok tool payloads from tripping grammar complexity#17404

Open
brendandebeasi wants to merge 6 commits intoanomalyco:devfrom
brendandebeasi:grok-xai-tool-budget-upstream
Open

fix: prevent xAI/Grok tool payloads from tripping grammar complexity#17404
brendandebeasi wants to merge 6 commits intoanomalyco:devfrom
brendandebeasi:grok-xai-tool-budget-upstream

Conversation

@brendandebeasi
Copy link
Copy Markdown

@brendandebeasi brendandebeasi commented Mar 13, 2026

Issue for this PR

Closes #17405

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Grok requests were still able to include oversized tool payloads even when activeTools was reduced, because the full tools map could still be sent to streamText. On xAI models this can trigger grammar is too complex failures.

This PR hardens the xAI path by:

  • capping xAI tools by count (40)
  • budgeting by estimated wire payload per tool (name + description + schema), not schema-only bytes
  • pruning the actual tools object sent to streamText for xAI so dropped tools cannot leak through
  • adding tests for schema-budget capping and dropping a single oversized tool when tool choice is auto

Why this works: xAI grammar complexity is sensitive to total tool schema payload; enforcing limits on the exact outgoing tool payload prevents over-budget requests from reaching Grok.

Related context:

  • Existing xAI schema-sanitization work in this repo (same failure family)
  • External reference: vercel/ai issue around xAI grammar complexity (vercel/ai#8024)

How did you verify your code works?

  • bun test test/session/llm.test.ts (packages/opencode) -> pass (12/12)
  • bun typecheck (packages/opencode) -> pass
  • Real CLI oneshot smoke test against a local mocked xAI /chat/completions endpoint:
    • invokes opencode run ... --model xai/grok-4
    • asserts outgoing tools payload stays within count <= 40 and bytes <= 20000
    • pass (CLI oneshot xAI smoke test passed, requests=1)

Screenshots / recordings

N/A (non-UI change)

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue and removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Mar 13, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

Brendan DeBeasi added 6 commits March 20, 2026 09:14
Estimate Grok tool budget using full wire payload size and prune the actual tool map sent to streamText so oversized grammars no longer slip through activeTools-only filtering.
Use a safe typed lookup for optional tool limits and adjust the cherry-picked xAI tests to the current Provider.getModel API in dev without changing runtime behavior.
Match existing session test patterns using providerID/modelID constants so CI typecheck resolves branded provider IDs consistently.
Use Provider.getModel parameter typing for xAI provider IDs in new tests to satisfy branded provider type checks in upstream CI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

xAI/Grok requests fail with 'grammar is too complex' when tool payloads are oversized

1 participant