Python: fix: prevent inner_exception from being lost in AgentFrameworkException (#5167)

* fix: prevent inner_exception from being lost in AgentFrameworkException

The __init__ method unconditionally called super().__init__() after
the conditional call with inner_exception, effectively overwriting the
exception args and losing the inner_exception reference.

Add else branch so super().__init__() is only called once with the
correct arguments.

Fixes #5155

Signed-off-by: bahtya <bahtyar153@qq.com>

* test: add explicit tests for AgentFrameworkException inner_exception handling

- test_exception_with_inner_exception: verifies args include inner exception
- test_exception_without_inner_exception: verifies args only contain message
- test_exception_inner_exception_none_explicit: verifies explicit None

Covers both branches of the if/else in __init__.

* fix: export AgentFrameworkException from package

Bahtya

---------

Signed-off-by: bahtya <bahtyar153@qq.com>
This commit is contained in:
bahtyar
2026-04-27 12:53:06 +08:00
committed by GitHub
Unverified
parent 56fb634f0e
commit dad3652f46
3 changed files with 33 additions and 1 deletions
@@ -247,6 +247,7 @@ from ._workflows._workflow_executor import (
WorkflowExecutor,
)
from .exceptions import (
AgentFrameworkException,
MiddlewareException,
UserInputRequiredException,
WorkflowCheckpointException,
@@ -276,6 +277,7 @@ __all__ = [
"USER_AGENT_TELEMETRY_DISABLED_ENV_VAR",
"Agent",
"AgentContext",
"AgentFrameworkException",
"AgentEvalConverter",
"AgentExecutor",
"AgentExecutorRequest",
@@ -34,7 +34,8 @@ class AgentFrameworkException(Exception):
logger.log(log_level, message, exc_info=inner_exception)
if inner_exception:
super().__init__(message, inner_exception, *args) # type: ignore
super().__init__(message, *args) # type: ignore
else:
super().__init__(message, *args) # type: ignore
# region Agent Exceptions
@@ -0,0 +1,29 @@
# Copyright (c) Microsoft. All rights reserved.
"""Tests for AgentFrameworkException inner_exception handling."""
import pytest
from agent_framework import AgentFrameworkException
def test_exception_with_inner_exception():
"""When inner_exception is provided, it should be set as the second arg."""
inner = ValueError("inner error")
exc = AgentFrameworkException("test message", inner_exception=inner)
assert exc.args[0] == "test message"
assert exc.args[1] is inner
def test_exception_without_inner_exception():
"""When inner_exception is None, args should only contain the message."""
exc = AgentFrameworkException("test message")
assert exc.args == ("test message",)
assert len(exc.args) == 1
def test_exception_inner_exception_none_explicit():
"""When inner_exception is explicitly None, args should only contain the message."""
exc = AgentFrameworkException("test message", inner_exception=None)
assert exc.args == ("test message",)
assert len(exc.args) == 1