Repository navigation
Python: Fix core docstring examples that use missing APIs - #9751
Naveen Chatlapalli (1aifanatic) wants to merge 2 commits into
Conversation
- Import ObservabilitySettings, get_tracer and get_meter from agent_framework.observability, and LabelTrackingFunctionMiddleware from agent_framework.security; the root package does not export them. - Pass client= to Agent and quarantine_chat_client= to SecureAgentConfig instead of chat_client=, and use Agent instead of ChatCompletionAgent. - Use ChatContext.client and retry AgentMiddleware on exceptions instead of the nonexistent chat_client and AgentResponse.is_error attributes. - Call raise_for_status() on each EvalResults returned by evaluate_agent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
The retry example must preserve explicit middleware abort and termination signals rather than retrying them.
1 open issue
What changed in this PR
This PR updates Python core docstring examples to match current Agent Framework APIs. It changes documentation only, not runtime implementations.
Changes:
- Correct public imports, constructor keywords, and middleware attribute access.
- Rewrite the retry example to handle raised exceptions.
- Check evaluation status on each returned result.
| File | Description |
|---|---|
python/packages/core/agent_framework/security.py |
Correct security imports and agent configuration examples. |
python/packages/core/agent_framework/observability.py |
Import observability APIs from their public module. |
python/packages/core/agent_framework/_middleware.py |
Update client access and exception-based retry examples. |
python/packages/core/agent_framework/_evaluation.py |
Call raise_for_status() on individual evaluation results. |
Review effort: Balanced
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| for result in results: | ||
| result.raise_for_status() | ||
|
|
||
| Local evaluator example: |
There was a problem hiding this comment.
Naveen Chatlapalli (@1aifanatic), this local example has another pre-existing problem: it calls tool_called_check without importing it, so copying it raises NameError before evaluation starts. Could you add tool_called_check to the agent_framework import here and in the LocalEvaluator basic-usage example, which has the same omission?
| .. code-block:: python | ||
|
|
||
| from agent_framework import Agent, LabelTrackingFunctionMiddleware, tool | ||
| from agent_framework import Agent, tool |
There was a problem hiding this comment.
Naveen Chatlapalli (@1aifanatic), the run call below was already broken before this PR: Agent.run leaves the OpenAI-style dictionary unchanged, and the OpenAI serializer then raises 'dict' object has no attribute 'role'. Could you import Message here and pass messages=[Message("user", ["What's the weather?"])], or simply use await agent.run("What's the weather?")?

Motivation & Context
Several code examples in
agent-framework-coredocstrings fail when copied. They import names the root package doesn't export, pass removed keyword arguments, or read attributes that don't exist:mainobservability.pyfrom agent_framework import ObservabilitySettings(alsoget_tracer,get_meter)ImportErrorsecurity.pyfrom agent_framework import LabelTrackingFunctionMiddlewareImportErrorsecurity.py(get_security_tools)Agent(chat_client=client, ...)TypeError: unexpected keyword argument 'chat_client'security.py(apply_mcp_security_labels)ChatCompletionAgent(chat_client=..., context_providers=[SecureAgentConfig(chat_client=client)])ChatCompletionAgentin Agent Framework;SecureAgentConfighas nochat_client_middleware.py(ChatContext)context.chat_clientAttributeError; the attribute isclient_middleware.py(AgentMiddleware)context.result.is_errorAttributeError;AgentResponsehas nois_error_evaluation.py(module docstring)results.raise_for_status()AttributeError;evaluate_agentreturnslist[EvalResults]Description & Review Guide
agent_framework.observabilityandagent_framework.security.client=toAgentandquarantine_chat_client=toSecureAgentConfig.context.clientin theChatContextexample.RetryMiddlewareto retrycall_next()when it raises.raise_for_status()on eachEvalResults, assamples/02-agents/evaluation/evaluate_agent.pydoes.RetryMiddlewarerewrite. I checked that callingcall_next()again after a failed attempt re-runs the chat client: with a client that fails once, the agent returns the second response after one retry.I found these by extracting the Python examples from the package docstrings and running pyright on them (
unknown import symbol,No parameter named,Cannot access attribute). I then ran each corrected form, and the original form where it fails, against the installed packages. The remaining pyright reports in these modules refer to helpers the examples define themselves, such asself.count_tokens. Related core tests pass (872 passed, 14 skipped).Related Issue
N/A - trivial change: docstring examples are corrected to the current API, with no behavior change.
AI Assistance
AI assistance details: Claude Code helped extract and type-check the docstring examples, verify the corrected forms at runtime, make the edits, and draft this description.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.🤖 Generated with Claude Code