Repository navigation
Python: Fix core docstring examples that use missing APIs #9751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -661,7 +661,7 @@ class ChatContext: | |
|
|
||
| class TokenCounterMiddleware(ChatMiddleware): | ||
| async def process(self, context: ChatContext, call_next): | ||
| print(f"Chat client: {context.chat_client.__class__.__name__}") | ||
| print(f"Chat client: {context.client.__class__.__name__}") | ||
| print(f"Messages: {len(context.messages)}") | ||
| print(f"Model: {context.options.get('model')}") | ||
|
|
||
|
|
@@ -859,19 +859,31 @@ class AgentMiddleware(ABC): | |
| Examples: | ||
| .. code-block:: python | ||
|
|
||
| from agent_framework import AgentMiddleware, AgentContext, Agent | ||
| from agent_framework import ( | ||
| Agent, | ||
| AgentContext, | ||
| AgentMiddleware, | ||
| MiddlewareFailure, | ||
| MiddlewareTermination, | ||
| ) | ||
|
|
||
|
|
||
| class RetryMiddleware(AgentMiddleware): | ||
| def __init__(self, max_retries: int = 3): | ||
| self.max_retries = max_retries | ||
|
|
||
| async def process(self, context: AgentContext, call_next): | ||
| for attempt in range(self.max_retries): | ||
| await call_next() | ||
| if context.result and not context.result.is_error: | ||
| break | ||
| print(f"Retry {attempt + 1}/{self.max_retries}") | ||
| for attempt in range(1, self.max_retries + 1): | ||
| try: | ||
| await call_next() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naveen Chatlapalli (@1aifanatic) This retry example does not retry streaming failures — In streaming mode, |
||
| return | ||
| except (MiddlewareFailure, MiddlewareTermination): | ||
| # Abort and stop signals must reach the caller unchanged. | ||
| raise | ||
| except Exception: | ||
|
eavanvalkenburg marked this conversation as resolved.
Resolved
|
||
| if attempt == self.max_retries: | ||
| raise | ||
| print(f"Retry {attempt}/{self.max_retries}") | ||
|
|
||
|
|
||
| # Use with an agent | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1343,7 +1343,8 @@ class LabelTrackingFunctionMiddleware(FunctionMiddleware, _SecurityScopeBinding) | |
| Examples: | ||
| .. code-block:: python | ||
|
|
||
| from agent_framework import Agent, LabelTrackingFunctionMiddleware, tool | ||
| from agent_framework import Agent, tool | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naveen Chatlapalli (@1aifanatic), the run call below was already broken before this PR:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done — the example now calls |
||
| from agent_framework.security import LabelTrackingFunctionMiddleware | ||
|
|
||
|
|
||
| @tool(additional_properties={"source_integrity": "trusted"}) | ||
|
|
@@ -1358,7 +1359,7 @@ async def get_weather(city: str) -> str: | |
| agent = Agent(client=client, name="assistant", tools=[get_weather], middleware=[middleware]) | ||
|
|
||
| # Run agent - untrusted tool results are automatically hidden | ||
| response = await agent.run(messages=[{"role": "user", "content": "What's the weather?"}]) | ||
| response = await agent.run("What's the weather?") | ||
| """ | ||
|
|
||
| _requires_session_state = True | ||
|
|
@@ -4314,7 +4315,7 @@ def get_security_tools() -> list[FunctionTool]: | |
| from agent_framework.security import get_security_tools | ||
|
|
||
| agent = Agent( | ||
| chat_client=client, | ||
| client=client, | ||
| instructions="You are a helpful assistant.", | ||
| tools=[my_tool, *get_security_tools()], | ||
| ) | ||
|
|
@@ -4467,10 +4468,10 @@ async def apply_mcp_security_labels( | |
|
|
||
| async with MCPStdioTool(name="github", command="gh-mcp", args=["stdio"]) as mcp: | ||
| await apply_mcp_security_labels(mcp) | ||
| agent = ChatCompletionAgent( | ||
| chat_client=client, | ||
| agent = Agent( | ||
| client=client, | ||
| tools=[mcp], | ||
| context_providers=[SecureAgentConfig(chat_client=client)], | ||
| context_providers=[SecureAgentConfig(quarantine_chat_client=client)], | ||
| ) | ||
| """ | ||
| if not getattr(mcp_tool, "is_connected", False): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naveen Chatlapalli (@1aifanatic), this local example has another pre-existing problem: it calls
tool_called_checkwithout importing it, so copying it raisesNameErrorbefore evaluation starts. Could you addtool_called_checkto theagent_frameworkimport here and in theLocalEvaluatorbasic-usage example, which has the same omission?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done — both local evaluator examples (module docstring and
LocalEvaluatorbasic usage) now importtool_called_check. I ran each against an agent on a stub chat client: evaluation completes and reports the expected tool-check failure instead of aNameError.