Copilot Chat: agent request crashes with "Cannot read properties of undefined (reading 'type')" in countToolTokens when a tool input schema uses top-level JSON-Schema if/then (BYOK / custom model)

Open
#323,102 0 comments 0 reactions 1 assignee View on GitHub

@vritant24 is already working on this.

Since Jun 26, 2026.

Assessment

This issue has not been assessed yet.

Description

Type: Bug

Summary

In agent mode with a custom / BYOK OpenAI-compatible model, a chat request aborts before sending whenever the active tool set includes an MCP tool whose input JSON Schema uses top-level if / then conditional keywords. The extension's local token counter (countToolTokenscountObjectTokenscountMessageObjectTokenstokenLength) dereferences .type on a schema node it doesn't recognise and throws TypeError: Cannot read properties of undefined (reading 'type'). "Try Again" replays the same tool set and fails identically.

This only reproduces with custom/BYOK models, where the extension counts tool tokens locally. First-party (Copilot-hosted) models offload token counting and are unaffected.

Environment
  • VS Code: 1.126.0 (commit 7e7950df89d055b5a378379db9ee14290772148a, 2026-06-23, stable)
  • Bundled Copilot extension: 0.54.0
  • OS: Linux (WSL2 remote)
  • Model: a custom OpenAI-compatible endpoint registered via BYOK (provider type "custom OpenAI-compatible" / "custom endpoint")
Steps to reproduce
  1. Register a custom OpenAI-compatible model via BYOK and select it in agent mode.
  2. Register an MCP server that exposes a tool whose input schema uses top-level if/then, e.g.:
    {
      "name": "run_query",
      "inputSchema": {
        "type": "object",
        "properties": { "query": { "type": "string" }, "mode": { "type": "string" } },
        "if":   { "properties": { "mode": { "const": "nl" } } },
        "then": { "required": ["query"] }
      }
    }
    
  3. Send any agent-mode request with that tool in scope.
Actual behaviour

The extension first logs (repeatedly, per request):

[warning] Tool <tool-name> failed validation: object has unsupported top-level schema keyword 'if'
[warning] Tool <tool-name> failed validation: object has unsupported top-level schema keyword 'then'

…then the request aborts with:

TypeError: Cannot read properties of undefined (reading 'type')
    at gre.tokenLength (<vscode-server>/bin/7e7950df.../extensions/copilot/dist/extension.js:1662:34964)
    at gre.countMessageObjectTokens (...:1662:36231)
    at gre.countMessageObjectTokens (...:1662:36504)
    at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
    at async gre.countMessageObjectTokens (...:1662:36493)
    at async gre.countObjectTokens (...:1662:36795)
    at async gre.countToolTokens (...:1662:35927)
    at async LG.countToolTokens (...:1690:16849)
    at async LG._provideLanguageModelResponse (...:1690:11650)
    at async LG.provideLanguageModelResponse (...:1690:15320)

Surfaced in the UI as: "Sorry, your request failed. Please try again."

Expected behaviour

Unsupported/unknown JSON-Schema keywords (if/then/else, etc.) should be ignored or stripped during token counting, not crash the request. tokenLength/countObjectTokens should be null-safe when walking schema nodes that have no type. At worst the tool should be skipped (as the validation warning implies), not abort the whole request.

Notes / workaround
  • Deselecting the offending tool in the agent-mode tools picker avoids the crash.
  • Top-level if/then is valid JSON Schema (Draft-07+); the failure is in how the token counter handles a schema node it chose not to support.
Impact

Any third-party MCP tool that ships a Draft-07 conditional schema makes custom/BYOK models unusable whenever that tool is in scope. Client request IDs from affected sessions available on request for telemetry correlation.

Dominant language
TypeScript
Stars
193k
Forks
42.9k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/vscode

All issues in microsoft/vscode

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.