Skip to content

Chat_sampler script bug #455

@arJ-V

Description

@arJ-V

The _print_stream function had a bug where if the iterator was empty
(no states were yielded), the variable 'state' would be undefined when
accessed outside the loop, causing a NameError.

Changes:

  • Track the last state during iteration in a separate variable (last_state)
  • Add explicit check for empty iterator case and raise ValueError with clear message
  • Preserve existing code structure with assignment to 'out' variable before return

This fixes a potential runtime error that could occur in edge cases where
the streaming iterator yields no states, such as early termination before
any states are produced.

Location: gemma/gm/text/_chat_sampler.py lines 223-240

I would be happy to make a pr for this issue for you to review. lmk!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions