Skip to content

fix: initialize task variable in RemoteAgent to prevent UnboundLocalError#2805

Merged
vvlrff merged 6 commits into
ag2ai:mainfrom
kimjune01:fix/task-unbound-error-2223
May 13, 2026
Merged

fix: initialize task variable in RemoteAgent to prevent UnboundLocalError#2805
vvlrff merged 6 commits into
ag2ai:mainfrom
kimjune01:fix/task-unbound-error-2223

Conversation

@kimjune01

Copy link
Copy Markdown
Contributor

Summary

  • Initialize task: Task | None = None before the event loop in RemoteAgent.a_generate_remote_reply
  • Prevents a latent UnboundLocalError when the message-only flow skips the else block that assigns task
  • Consistent with the _ask_streaming/_ask_polling patterns elsewhere in the codebase

Fixes #2223

Test plan

  • Added test_task_initialization.py with MockClient to verify message-only flow completes without error
  • All 12 existing test_client.py tests pass
  • Fixed original test (replaced broken MessageOnlyClient that did not conform to ClientFactory protocol) with proper MockClient

kimjune01 added 2 commits May 11, 2026 21:19
Fixes ag2ai#2223

Initialize `task` to None before the event processing loop in
`RemoteAgent.a_generate_remote_reply()`, matching the pattern used in
`_ask_streaming()` and `_ask_polling()` methods.

This prevents potential UnboundLocalError if the task variable is
referenced in error handlers or debugging code when all events in the
stream are Message types (no Task events).

Changes:
- Initialize `task: Task | None = None` before the async for loop
- Add test for message-only flow scenario

The fix is defensive programming that aligns with existing code patterns
and prevents subtle bugs in error scenarios.
MessageOnlyClient did not conform to ClientFactory protocol (not
callable, no async context manager). Use the existing MockClient
factory which already produces message-only responses, and follow
the test patterns established in test_client.py.
@kimjune01 kimjune01 requested a review from Lancetnik as a code owner May 12, 2026 05:59
@CLAassistant

CLAassistant commented May 12, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added the A2A label May 12, 2026
@vvlrff vvlrff self-assigned this May 12, 2026
@vvlrff

vvlrff commented May 12, 2026

Copy link
Copy Markdown
Collaborator

@kimjune01 Hi, thanks for pr! Can you sign CLA?

@codecov

codecov Bot commented May 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
autogen/a2a/client.py 65.05% <100.00%> (+0.18%) ⬆️

... and 54 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vvlrff vvlrff added this pull request to the merge queue May 13, 2026
Merged via the queue into ag2ai:main with commit 9d46c79 May 13, 2026
19 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.

[Bug]: UnboundLocalError: cannot access local variable 'task' where it is not associated with a value

4 participants