Python: Fix broken Content API imports in Python samples (#3639)

* Initial plan

* Fix broken import paths for Content API in all Python sample files

Co-authored-by: moonbox3 <35585003+moonbox3@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: moonbox3 <35585003+moonbox3@users.noreply.github.com>
This commit is contained in:
Copilot
2026-02-03 22:48:26 +00:00
committed by GitHub
Unverified
parent f56218fa1e
commit 96d3f2a55e
11 changed files with 30 additions and 30 deletions
@@ -8,9 +8,9 @@ from agent_framework import (
AgentResponseUpdate,
ChatAgent,
CitationAnnotation,
Content,
HostedCodeInterpreterTool,
HostedFileContent,
TextContent,
tool,
)
from agent_framework.azure import AzureAIProjectAgentProvider
@@ -138,7 +138,7 @@ async def non_streaming_example() -> None:
# AgentResponse has messages property, which contains ChatMessage objects
for message in result.messages:
for content in message.contents:
if isinstance(content, TextContent) and content.annotations:
if content.type == "text" and content.annotations:
for annotation in content.annotations:
if isinstance(annotation, CitationAnnotation) and annotation.file_id:
annotations_found.append(annotation)
@@ -181,7 +181,7 @@ async def streaming_example() -> None:
async for update in agent.run_stream(QUERY):
if isinstance(update, AgentResponseUpdate):
for content in update.contents:
if isinstance(content, TextContent):
if content.type == "text":
if content.text:
text_chunks.append(content.text)
if content.annotations:
@@ -2,7 +2,7 @@
import asyncio
from agent_framework import ChatMessage, TextContent, UriContent
from agent_framework import ChatMessage, Content
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
@@ -27,8 +27,8 @@ async def main():
user_message = ChatMessage(
role="user",
contents=[
TextContent(text="What do you see in this image?"),
UriContent(
Content.from_text(text="What do you see in this image?"),
Content.from_uri(
uri="https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
media_type="image/jpeg",
),
@@ -10,8 +10,8 @@ from agent_framework import (
AgentThread,
BaseAgent,
ChatMessage,
Content,
Role,
TextContent,
tool,
)
@@ -78,7 +78,7 @@ class EchoAgent(BaseAgent):
if not normalized_messages:
response_message = ChatMessage(
role=Role.ASSISTANT,
contents=[TextContent(text="Hello! I'm a custom echo agent. Send me a message and I'll echo it back.")],
contents=[Content.from_text(text="Hello! I'm a custom echo agent. Send me a message and I'll echo it back.")],
)
else:
# For simplicity, echo the last user message
@@ -88,7 +88,7 @@ class EchoAgent(BaseAgent):
else:
echo_text = f"{self.echo_prefix}[Non-text message received]"
response_message = ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text=echo_text)])
response_message = ChatMessage(role=Role.ASSISTANT, contents=[Content.from_text(text=echo_text)])
# Notify the thread of new messages if provided
if thread is not None:
@@ -133,7 +133,7 @@ class EchoAgent(BaseAgent):
chunk_text = f" {word}" if i > 0 else word
yield AgentResponseUpdate(
contents=[TextContent(text=chunk_text)],
contents=[Content.from_text(text=chunk_text)],
role=Role.ASSISTANT,
)
@@ -142,7 +142,7 @@ class EchoAgent(BaseAgent):
# Notify the thread of the complete response if provided
if thread is not None:
complete_response = ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text=response_text)])
complete_response = ChatMessage(role=Role.ASSISTANT, contents=[Content.from_text(text=response_text)])
await self._notify_thread_of_new_messages(thread, normalized_messages, complete_response)
@@ -11,8 +11,8 @@ from agent_framework import (
ChatMessage,
ChatResponse,
ChatResponseUpdate,
Content,
Role,
TextContent,
use_chat_middleware,
use_function_invocation,
tool,
@@ -77,7 +77,7 @@ class EchoingChatClient(BaseChatClient[TOptions_co], Generic[TOptions_co]):
else:
response_text = f"{self.prefix} [No text message found]"
response_message = ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text=response_text)])
response_message = ChatMessage(role=Role.ASSISTANT, contents=[Content.from_text(text=response_text)])
return ChatResponse(
messages=[response_message],
@@ -103,7 +103,7 @@ class EchoingChatClient(BaseChatClient[TOptions_co], Generic[TOptions_co]):
# Stream character by character
for char in response_text:
yield ChatResponseUpdate(
contents=[TextContent(text=char)],
contents=[Content.from_text(text=char)],
role=Role.ASSISTANT,
response_id=f"echo-stream-resp-{random.randint(1000, 9999)}",
model_id="echo-model-v1",
@@ -2,7 +2,7 @@
import asyncio
from agent_framework import ChatMessage, TextContent, UriContent
from agent_framework import ChatMessage, Content
from agent_framework.openai import OpenAIResponsesClient
"""
@@ -26,8 +26,8 @@ async def main():
user_message = ChatMessage(
role="user",
contents=[
TextContent(text="What do you see in this image?"),
UriContent(
Content.from_text(text="What do you see in this image?"),
Content.from_uri(
uri="https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
media_type="image/jpeg",
),
@@ -3,7 +3,7 @@
import asyncio
import base64
from agent_framework import DataContent, HostedImageGenerationTool, ImageGenerationToolResultContent, UriContent
from agent_framework import Content, HostedImageGenerationTool, ImageGenerationToolResultContent
from agent_framework.openai import OpenAIResponsesClient
"""
@@ -72,7 +72,7 @@ async def main() -> None:
for content in message.contents:
if isinstance(content, ImageGenerationToolResultContent) and content.outputs:
for output in content.outputs:
if isinstance(output, (DataContent, UriContent)) and output.uri:
if output.type in ("data", "uri") and output.uri:
show_image_info(output.uri)
break
@@ -4,7 +4,7 @@ import asyncio
import base64
import anyio
from agent_framework import DataContent, HostedImageGenerationTool
from agent_framework import Content, HostedImageGenerationTool
from agent_framework.openai import OpenAIResponsesClient
"""OpenAI Responses Client Streaming Image Generation Example
@@ -72,7 +72,7 @@ async def main():
# Handle partial images
# The final partial image IS the complete, full-quality image. Each partial
# represents a progressive refinement, with the last one being the finished result.
if isinstance(content, DataContent) and content.additional_properties.get("is_partial_image"):
if content.type == "data" and content.additional_properties.get("is_partial_image"):
print(f" Image {image_count} received")
# Extract file extension from media_type (e.g., "image/png" -> "png")
@@ -6,8 +6,8 @@ from agent_framework import (
ChatAgent,
CodeInterpreterToolCallContent,
CodeInterpreterToolResultContent,
Content,
HostedCodeInterpreterTool,
TextContent,
tool,
)
from agent_framework.openai import OpenAIResponsesClient
@@ -41,13 +41,13 @@ async def main() -> None:
if code_blocks:
code_inputs = code_blocks[0].inputs or []
for content in code_inputs:
if isinstance(content, TextContent):
if content.type == "text":
print(f"Generated code:\n{content.text}")
break
if outputs:
print("Execution outputs:")
for out in outputs[0].outputs or []:
if isinstance(out, TextContent):
if out.type == "text":
print(out.text)
@@ -12,9 +12,9 @@ from agent_framework import (
ChatMessage,
ChatResponse,
ChatResponseUpdate,
Content,
FunctionInvocationContext,
Role,
TextContent,
tool,
chat_middleware,
function_middleware,
@@ -57,7 +57,7 @@ async def security_filter_middleware(
# Streaming mode: return async generator
async def blocked_stream() -> AsyncIterable[ChatResponseUpdate]:
yield ChatResponseUpdate(
contents=[TextContent(text=error_message)],
contents=[Content.from_text(text=error_message)],
role=Role.ASSISTANT,
)
@@ -10,8 +10,8 @@ from agent_framework import (
AgentResponseUpdate,
AgentRunContext,
ChatMessage,
Content,
Role,
TextContent,
tool,
)
from agent_framework.azure import AzureAIAgentClient
@@ -69,7 +69,7 @@ async def weather_override_middleware(
# For streaming: create an async generator that yields chunks
async def override_stream() -> AsyncIterable[AgentResponseUpdate]:
for chunk in chunks:
yield AgentResponseUpdate(contents=[TextContent(text=chunk)])
yield AgentResponseUpdate(contents=[Content.from_text(text=chunk)])
context.result = override_stream()
else:
@@ -32,12 +32,12 @@ from contextlib import asynccontextmanager
from agent_framework import (
AgentRunUpdateEvent,
ChatAgent,
Content,
HandoffAgentUserRequest,
HandoffBuilder,
HostedCodeInterpreterTool,
HostedFileContent,
RequestInfoEvent,
TextContent,
WorkflowEvent,
WorkflowRunState,
WorkflowStatusEvent,
@@ -76,7 +76,7 @@ def _handle_events(events: list[WorkflowEvent]) -> tuple[list[RequestInfoEvent],
if isinstance(content, HostedFileContent):
file_ids.append(content.file_id)
print(f"[Found HostedFileContent: file_id={content.file_id}]")
elif isinstance(content, TextContent) and content.annotations:
elif content.type == "text" and content.annotations:
for annotation in content.annotations:
if hasattr(annotation, "file_id") and annotation.file_id:
file_ids.append(annotation.file_id)