Skip to content

Fix "Failed to parse grammar" error for requests without grammar field#80

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-failed-parse-grammar
Draft

Fix "Failed to parse grammar" error for requests without grammar field#80
Copilot wants to merge 4 commits intomainfrom
copilot/fix-failed-parse-grammar

Conversation

Copy link

Copilot AI commented Dec 24, 2025

OpenAI-compatible clients sending chat completion requests without a grammar field receive "Failed to parse grammar" errors, blocking integration with tools like opencode.

Root Cause

Two issues in the grammar parameter handling:

  1. Missing grammar field defaults to server's global config instead of empty string
  2. Empty grammar strings passed to common_sampler_init cause nullptr returns

Changes

server-task.cpp line 281: Use empty string default instead of inheriting from server config

// Before: inherits from server configuration
params.sampling.grammar = json_value(data, "grammar", defaults.sampling.grammar);

// After: defaults to empty string
params.sampling.grammar = json_value(data, "grammar", std::string());

server-context.cpp lines 1058-1065: Clear grammar-related fields when grammar is empty before sampler initialization

// Clear grammar triggers and lazy flag when grammar string is empty
if (task.params.sampling.grammar.empty()) {
    task.params.sampling.grammar_triggers.clear();
    task.params.sampling.grammar_lazy = false;
}

This ensures common_sampler_init doesn't attempt to parse empty grammar strings while preserving behavior for requests with explicit grammar constraints.

Original prompt

This section details on the original issue you should resolve

<issue_title>Failed to parse grammar - opencode</issue_title>
<issue_description>When I try to connect opencode with the docker model I only get this answer:
Failed to parse grammar

My open code configuration is:

"dmr": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Docker Model Runner",
      "options": {
        "baseURL": "http://localhost:12434/engines/v1"
      },
      "models": {
        "ai/qwen3-coder": {
          "name": "Qwen3 Coder"
        }
      }
    }

I use a macbook pro and have installed Docker model with version: Docker Model Runner version v1.0.2

I also tried it using my new gigabyte AI TOP ATOM.

"dmr": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Docker Model Runner",
      "options": {
        "baseURL": "http://localhost:12434/engines/v1"
      },
      "models": {
        "ai/qwen3-coder": {
          "name": "Qwen3 Coder"
        }
      }
    }

With Docker Model Runner version v1.0.6

I followed the instruction based on: www.docker.com/blog/new-nvidia-spark-docker-model-runner/

I hope someone can help me out

</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits December 24, 2025 14:16
- Change default grammar value from server config to empty string in server-task.cpp
- Clear grammar-related fields when grammar is empty before sampler initialization
- Prevents common_sampler_init from failing on empty grammar strings from OpenAI-compatible clients

Co-authored-by: ericcurtin <1694275+ericcurtin@users.noreply.github.com>
Co-authored-by: ericcurtin <1694275+ericcurtin@users.noreply.github.com>
Co-authored-by: ericcurtin <1694275+ericcurtin@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix grammar parsing issue with opencode Fix "Failed to parse grammar" error for requests without grammar field Dec 24, 2025
Copilot AI requested a review from ericcurtin December 24, 2025 14:22
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.

Failed to parse grammar - opencode

2 participants