Skip to content

Conversation

@emattiza
Copy link
Contributor

@emattiza emattiza commented Jan 5, 2026

Description

At runtime, when the Gemini model returns an empty async event stream, the current code references local variables only declared and initialized in the body of the event stream for loop. This cause an UnboundLocal exception to be raised consistent with #1402

Related Issues

#1402

Documentation PR

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare
  • I have introduced a unit test with the specific failure state from gemini
  • I have confirmed the test fails without the proposed source changes
  • I have confirmed the test now passes with the proposed source changes

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

emattiza and others added 2 commits December 31, 2025 14:16
Initialize candidate and event variables to None to prevent
UnboundLocalError when stream yields no events. Also conditionally
yield metadata only when event exists.
Add test case that reproduces UnboundLocalError when Gemini stream
yields no events. Without proper initialization of candidate and event
variables, an empty stream causes UnboundLocalError.
Copy link
Member

@dbschmigelski dbschmigelski left a comment

Choose a reason for hiding this comment

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

Hi, thanks for raising this! I am inclined to approve to fix the immediate issue.

I am curious if this is emitting the contentBlockStart is incorrect in this scenario though. It does appear consistent with what we are doing with other models though.

@codecov
Copy link

codecov bot commented Jan 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@emattiza
Copy link
Contributor Author

emattiza commented Jan 5, 2026

Hi, thanks for raising this! I am inclined to approve to fix the immediate issue.

I am curious if this is emitting the contentBlockStart is incorrect in this scenario though. It does appear consistent with what we are doing with other models though.

Yeah this seems very weird edge case wise and there may be some ulterior reason why the event stream is empty. FWIW, it ran 32k invocations through this workflow across tool calls and hit this maybe once or twice, so may chalk up to llm silliness that this ever happens to begin with.

Thank you!

@dbschmigelski dbschmigelski merged commit 1e27d79 into strands-agents:main Jan 7, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants