Repository navigation
Python: Fix core docstring examples that use missing APIs - #9751
Draft
Naveen Chatlapalli (1aifanatic) wants to merge 2 commits into
Draft
Naveen Chatlapalli (1aifanatic) wants to merge 2 commits into
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>
Naveen Chatlapalli (1aifanatic)
temporarily deployed
to
github-app-auth
October 27, 2026 12:39 — with
GitHub Actions
Inactive
Naveen Chatlapalli (1aifanatic)
temporarily deployed
to
github-app-auth
October 27, 2026 12:39 — with
GitHub Actions
Inactive
Contributor
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
| try: | ||
| await call_next() | ||
| return | ||
| except Exception: |
Contributor
Author
There was a problem hiding this comment.
Good catch — the docs for MiddlewareFailure say middleware must not swallow it. The example now re-raises MiddlewareFailure and MiddlewareTermination before the generic retry handler (both imported from agent_framework). Checked by running the example class directly: a transient RuntimeError is retried once and then succeeds, and either signal propagates after a single call.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Naveen Chatlapalli (1aifanatic)
temporarily deployed
to
github-app-auth
October 27, 2026 15:13 — with
GitHub Actions
Inactive
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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