fix(agentchat): retry MagenticOne ledger parsing when next_speaker is missing#7913
fix(agentchat): retry MagenticOne ledger parsing when next_speaker is missing#7913nolanchic wants to merge 1 commit into
Conversation
… missing In MagenticOneOrchestrator._orchestrate_step, when the model returns a valid progress-ledger JSON object that is missing the next_speaker field (or the field is malformed), the required_keys loop correctly sets key_error = True and breaks, intending to trigger a retry. However, the next_speaker validation that runs after the loop unconditionally accesses progress_ledger["next_speaker"]["answer"]. When next_speaker is the missing key this raises KeyError, which is not caught by the surrounding 'except (json.JSONDecodeError, TypeError)' and crashes the orchestrator instead of retrying up to _max_json_retries. Add a 'not key_error' guard so the next_speaker validation is skipped when a required key is already missing, letting the retry path run as designed. Adds a regression test that feeds a ledger missing next_speaker and asserts the team run completes via retry.
|
@microsoft-github-policy-service agree |
|
@nolanchic the command you issued was incorrect. Please try again. Examples are: and |
|
@microsoft-github-policy-service agree |
|
While working on this, I want to flag a related behavior boundary I intentionally did not change, in case reviewers want to weigh in. There are two distinct "next_speaker problem" cases in
The second case is pre-existing behavior (the But if maintainers prefer symmetry — i.e. the "speaker not in participants" case should also count as a parse failure and trigger a retry — that's a one-line change: replace the |
Why are these changes needed?
In
MagenticOneOrchestrator._orchestrate_step, when the model returns a valid progress-ledger JSON object that is missing thenext_speakerfield (or the field is malformed), thefor key in required_keysloop correctly setskey_error = Trueandbreaks — intending to trigger a retry.However, the
next_speakervalidation that runs after that loop unconditionally accessesprogress_ledger["next_speaker"]["answer"]:When
next_speakeris the missing key, this raisesKeyError, which is not caught by the surroundingexcept (json.JSONDecodeError, TypeError). The exception propagates and crashes the orchestrator instead of retrying up to_max_json_retries(10) as designed.This is a real-world failure mode: some models occasionally omit a field from the progress-ledger JSON. The existing retries exist precisely to tolerate this, but the unhandled
KeyErrorbypasses them.Fix
Add a
not key_errorguard so thenext_speakervalidation is skipped when a required key is already missing/malformed, letting the retry path run as designed:Related issue number
N/A — no existing issue covers this code path. (Related issues #6599 / #6547 describe a different root cause — markdown-wrapped JSON — which was already fixed via
extract_json_from_str.) Happy to open a tracking issue if the maintainers prefer.Checks
test_magentic_one_group_chat_ledger_missing_next_speaker_retries, which feeds a ledger missingnext_speakerand asserts the team run completes via retry instead of raisingKeyError. This test fails onmainwithKeyError: 'next_speaker'and passes with the fix.)Local verification