From f5b3ca4efbbaf33024a95e8d91c2ba8fe14061c5 Mon Sep 17 00:00:00 2001 From: bahtya Date: Wed, 8 Apr 2026 20:45:36 +0800 Subject: [PATCH 1/2] 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 --- python/packages/core/agent_framework/exceptions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/packages/core/agent_framework/exceptions.py b/python/packages/core/agent_framework/exceptions.py index 4f56c34b5c..d5a54c2102 100644 --- a/python/packages/core/agent_framework/exceptions.py +++ b/python/packages/core/agent_framework/exceptions.py @@ -34,7 +34,8 @@ def __init__( 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 From 649bfb9864ce5a4eb1fe0e14d19b02212b8db57a Mon Sep 17 00:00:00 2001 From: bahtya Date: Mon, 13 Apr 2026 20:33:48 +0800 Subject: [PATCH 2/2] 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__. --- .../core/tests/core/test_exceptions.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 python/packages/core/tests/core/test_exceptions.py diff --git a/python/packages/core/tests/core/test_exceptions.py b/python/packages/core/tests/core/test_exceptions.py new file mode 100644 index 0000000000..47b3a53197 --- /dev/null +++ b/python/packages/core/tests/core/test_exceptions.py @@ -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