Skip to content

Fix UnboundLocalError in generate_gate when generator yields nothing#3838

Open
Chessing234 wants to merge 1 commit intolm-sys:mainfrom
Chessing234:fix/issue-3786-generate-gate-unbound-local
Open

Fix UnboundLocalError in generate_gate when generator yields nothing#3838
Chessing234 wants to merge 1 commit intolm-sys:mainfrom
Chessing234:fix/issue-3786-generate-gate-unbound-local

Conversation

@Chessing234
Copy link
Copy Markdown

Summary

  • Fixes UnboundLocalError in ModelWorker.generate_gate() when generate_stream_gate() yields no items
  • Initializes x = b"{}\0" before the loop so the variable is always defined, returning an empty dict as a safe default

Details

In fastchat/serve/model_worker.py, the generate_gate method iterates over generate_stream_gate(params) and uses the last yielded value x after the loop. If the generator yields nothing, x is never assigned, causing an UnboundLocalError.

Fixes #3786

Test plan

  • Verify that generate_gate no longer crashes when generate_stream_gate yields no items
  • Verify normal behavior is unchanged when the generator does yield items

🤖 Generated with Claude Code

Initialize x before the loop to prevent UnboundLocalError if
generate_stream_gate yields no items.

Fixes lm-sys#3786

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

UnboundLocalError in generate_gate if generator yields nothing

1 participant