Skip to content

Add -g for specifying a GBNF grammar#2

Merged
artob merged 2 commits into
masterfrom
alex/gbnf
Nov 11, 2025
Merged

Add -g for specifying a GBNF grammar#2
artob merged 2 commits into
masterfrom
alex/gbnf

Conversation

@oleksandrvoyager

Copy link
Copy Markdown
Member

#1

@oleksandrvoyager oleksandrvoyager added the enhancement New feature or request label Nov 11, 2025

@isaac-asimov-bot isaac-asimov-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ CI failed

Show all
Platform Name Status Details
All Formatting Details
macOS Build library Details
macOS Build library (no default features) Details
macOS Build binaries Details
macOS Build tests Details
macOS Build examples Details
macOS Run tests Details
macOS Run doctests Details
Linux Build library Details
Linux Build library (no default features) Details
Linux Build binaries Details
Linux Build tests Details
Linux Build examples Details
Linux Run tests Details
Linux Run doctests Details
Windows Build library Details
Windows Build library (no default features) Details
Windows Build binaries Details
Windows Build tests Details
Windows Build examples Details
Windows Run tests Details
Windows Run doctests Details

=============================================

Platform Name Status Details
All Formatting Details

@artob

artob commented Nov 11, 2025

Copy link
Copy Markdown
Member

@race-of-sloths score 5

@race-of-sloths

race-of-sloths commented Nov 11, 2025

Copy link
Copy Markdown

@oleksandrvoyager Thank you for your contribution! Your pull request is now a part of the Race of Sloths!
Do you want to apply for monthly streak? Get 8+ score for a single PR this month and receive boost for race-of-sloths!

Shows inviting banner with latest news.

Shows profile picture for the author of the PR

Current status: executed
Reviewer Score
@artob 5

Your contribution is much appreciated with a final score of 5!
You have received 50 Sloth points for this contribution

@artob received 25 Sloth Points for reviewing and scoring this pull request.

What is the Race of Sloths

Race of Sloths is a friendly competition where you can participate in challenges and compete with other open-source contributors within your normal workflow

For contributors:

  • Tag @race-of-sloths inside your pull requests
  • Wait for the maintainer to review and score your pull request
  • Check out your position in the Leaderboard
  • Keep weekly and monthly streaks to reach higher positions
  • Boast your contributions with a dynamic picture of your Profile

For maintainers:

  • Score pull requests that participate in the Race of Sloths and receive a reward
  • Engage contributors with fair scoring and fast responses so they keep their streaks
  • Promote the Race to the point where the Race starts promoting you
  • Grow the community of your contributors

Feel free to check our website for additional details!

Bot commands
  • For contributors
    • Include a PR: @race-of-sloths include to enter the Race with your PR
  • For maintainers:
    • Invite contributor @race-of-sloths invite to invite the contributor to participate in a race or include it, if it's already a runner.
    • Assign points: @race-of-sloths score [1/2/3/5/8/13] to award points based on your assessment.
    • Reject this PR: @race-of-sloths exclude to send this PR back to the drawing board.
    • Exclude repo: @race-of-sloths pause to stop bot activity in this repo until @race-of-sloths unpause command is called

@artob
artob merged commit 875772f into master Nov 11, 2025
11 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in Platform Launch Nov 11, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for specifying a GBNF grammar file to constrain model decoding via the -g flag. The implementation removes the bon builder pattern dependency in favor of a simpler struct initialization approach.

Key changes:

  • Added -g/--grammar CLI option to specify GBNF grammar files
  • Added grammar field to Options struct with file reading logic
  • Removed bon dependency and its builder pattern
  • Added documentation showing GBNF usage example

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/prompter/main.rs Added CLI options for grammar, input, and output files; implemented grammar file reading
src/lib.rs Removed bon builder, added grammar field to Options struct, modified request to include grammar parameter
Cargo.toml Removed bon dependency
Cargo.lock Removed bon and related transitive dependencies
README.md Added GBNF grammar usage example with documentation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/lib.rs
Comment on lines +32 to +34
if let Value::Object(obj) = &mut req {
obj.insert("grammar".into(), Value::String(g.clone()));
}

Copilot AI Nov 11, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The json! macro creates an immutable Value, but you're attempting to mutate it. The let mut req declaration is necessary, but the pattern match on line 32 is checking the type unnecessarily since json!({...}) always produces a Value::Object.

Consider simplifying to:

let mut req = json!({
    "model": options.model,
    "messages": [{ "role": "user", "content": input.as_ref() }],
    "stream": false
});

if let Some(g) = &options.grammar {
    req["grammar"] = Value::String(g.clone());
}

This is cleaner and avoids the unnecessary type check.

Suggested change
if let Value::Object(obj) = &mut req {
obj.insert("grammar".into(), Value::String(g.clone()));
}
req["grammar"] = Value::String(g.clone());

Copilot uses AI. Check for mistakes.
@oleksandrvoyager oleksandrvoyager linked an issue Nov 11, 2025 that may be closed by this pull request
@oleksandrvoyager
oleksandrvoyager deleted the alex/gbnf branch November 11, 2025 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Support -g for specifying a GBNF grammar

4 participants