Skip to content

Commit 07a83a7

Browse files
Python: Recommend include_detailed_errors only when it is disabled (#9472)
The tool-failure warning always told callers to set include_detailed_errors=True, even when it was already enabled. Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent ce214c0 commit 07a83a7

2 files changed

Lines changed: 67 additions & 5 deletions

File tree

‎python/packages/core/agent_framework/_tools.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,21 +1998,23 @@ def _function_execution_error_result(
19981998
) -> Content:
19991999
from .observability import OBSERVABILITY_SETTINGS
20002000

2001+
include_detailed_errors = config.get("include_detailed_errors", False)
2002+
recommendation = "" if include_detailed_errors else " Set include_detailed_errors=True for the full detail."
20012003
if OBSERVABILITY_SETTINGS.SENSITIVE_DATA_ENABLED:
20022004
logger.warning(
2003-
"Function '%s' raised an exception; returning an error result to the model. "
2004-
"Set include_detailed_errors=True for the full detail. Exception: %r",
2005+
"Function '%s' raised an exception; returning an error result to the model.%s Exception: %r",
20052006
tool_name,
2007+
recommendation,
20062008
exception,
20072009
)
20082010
else:
20092011
logger.warning(
2010-
"Function '%s' raised an exception; returning an error result to the model. "
2011-
"Set include_detailed_errors=True for the full detail.",
2012+
"Function '%s' raised an exception; returning an error result to the model.%s",
20122013
tool_name,
2014+
recommendation,
20132015
)
20142016
message = "Error: Function failed."
2015-
if config.get("include_detailed_errors", False):
2017+
if include_detailed_errors:
20162018
message = f"{message} Exception: {exception}"
20172019
return _finalize_function_result(
20182020
call_id=function_call.call_id, # type: ignore[arg-type]

‎python/packages/core/tests/core/test_function_invocation_logic.py‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6023,6 +6023,66 @@ def make_result() -> dict[str, str]:
60236023
]
60246024
assert len(execution_warnings) == 1
60256025
assert (_PRIVATE_ERROR_DETAIL in execution_warnings[0]) is enable_sensitive_data
6026+
assert ("Set include_detailed_errors=True" in execution_warnings[0]) is not include_detailed_errors
6027+
6028+
6029+
@pytest.mark.parametrize(
6030+
("enable_instrumentation", "enable_sensitive_data"),
6031+
[(False, False), (True, False), (True, True)],
6032+
indirect=True,
6033+
)
6034+
@pytest.mark.usefixtures("span_exporter")
6035+
@pytest.mark.parametrize("include_detailed_errors", [False, True])
6036+
async def test_function_execution_error_warning_recommends_detailed_errors_only_when_disabled(
6037+
chat_client_base: SupportsChatGetResponse,
6038+
monkeypatch: pytest.MonkeyPatch,
6039+
caplog: pytest.LogCaptureFixture,
6040+
include_detailed_errors: bool,
6041+
enable_sensitive_data: bool,
6042+
) -> None:
6043+
@tool(name="error_function", approval_mode="never_require")
6044+
def error_func() -> str:
6045+
raise RuntimeError(f"Tool failure: {_PRIVATE_ERROR_DETAIL}")
6046+
6047+
monkeypatch.setattr(
6048+
chat_client_base,
6049+
"run_responses",
6050+
[
6051+
ChatResponse(
6052+
messages=Message(
6053+
role="assistant",
6054+
contents=[Content.from_function_call(call_id="1", name="error_function", arguments="{}")],
6055+
)
6056+
),
6057+
ChatResponse(messages=Message(role="assistant", contents=["done"])),
6058+
],
6059+
)
6060+
monkeypatch.setitem(
6061+
chat_client_base.function_invocation_configuration, # type: ignore[attr-defined] # ty: ignore[unresolved-attribute]
6062+
"include_detailed_errors",
6063+
include_detailed_errors,
6064+
)
6065+
6066+
with caplog.at_level(logging.WARNING, logger="agent_framework"):
6067+
response = await chat_client_base.get_response(
6068+
[Message(role="user", contents=["hello"])], options={"tools": [error_func]}
6069+
)
6070+
6071+
result = next(content for msg in response.messages for content in msg.contents if content.type == "function_result")
6072+
assert (_PRIVATE_ERROR_DETAIL in (result.result or "")) is include_detailed_errors
6073+
execution_warnings = [
6074+
record.getMessage()
6075+
for record in caplog.records
6076+
if record.name == "agent_framework"
6077+
and record.levelno == logging.WARNING
6078+
and "raised an exception; returning an error result" in record.getMessage()
6079+
]
6080+
assert len(execution_warnings) == 1
6081+
assert execution_warnings[0].startswith(
6082+
"Function 'error_function' raised an exception; returning an error result to the model."
6083+
)
6084+
assert ("Set include_detailed_errors=True" in execution_warnings[0]) is not include_detailed_errors
6085+
assert (_PRIVATE_ERROR_DETAIL in execution_warnings[0]) is enable_sensitive_data
60266086

60276087

60286088
async def test_function_invocation_config_include_detailed_errors_false(chat_client_base: SupportsChatGetResponse):

0 commit comments

Comments
 (0)