mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: [Breaking] Simplified Content types to a single class with classmethod constructors. (#3252)
* ported Content to a new model * fixed linting * fixes * fixed data format handling * fix for 3.10 mypy * fix * fix int test
This commit is contained in:
committed by
GitHub
Unverified
parent
73761aa4a3
commit
83e6229c11
@@ -321,7 +321,7 @@ class InMemoryConversationStore(ConversationStore):
|
||||
# Convert ChatMessage contents to OpenAI TextContent format
|
||||
message_content = []
|
||||
for content_item in msg.contents:
|
||||
if hasattr(content_item, "type") and content_item.type == "text":
|
||||
if content_item.type == "text":
|
||||
# Extract text from TextContent object
|
||||
text_value = getattr(content_item, "text", "")
|
||||
message_content.append(TextContent(type="text", text=text_value))
|
||||
|
||||
@@ -7,7 +7,7 @@ import logging
|
||||
from collections.abc import AsyncGenerator
|
||||
from typing import Any
|
||||
|
||||
from agent_framework import AgentProtocol
|
||||
from agent_framework import AgentProtocol, Content
|
||||
from agent_framework._workflows._events import RequestInfoEvent
|
||||
|
||||
from ._conversations import ConversationStore, InMemoryConversationStore
|
||||
@@ -602,7 +602,7 @@ class AgentFrameworkExecutor:
|
||||
"""
|
||||
# Import Agent Framework types
|
||||
try:
|
||||
from agent_framework import ChatMessage, DataContent, Role, TextContent
|
||||
from agent_framework import ChatMessage, Role
|
||||
except ImportError:
|
||||
# Fallback to string extraction if Agent Framework not available
|
||||
return self._extract_user_message_fallback(input_data)
|
||||
@@ -613,14 +613,12 @@ class AgentFrameworkExecutor:
|
||||
|
||||
# Handle OpenAI ResponseInputParam (List[ResponseInputItemParam])
|
||||
if isinstance(input_data, list):
|
||||
return self._convert_openai_input_to_chat_message(input_data, ChatMessage, TextContent, DataContent, Role)
|
||||
return self._convert_openai_input_to_chat_message(input_data, ChatMessage, Role)
|
||||
|
||||
# Fallback for other formats
|
||||
return self._extract_user_message_fallback(input_data)
|
||||
|
||||
def _convert_openai_input_to_chat_message(
|
||||
self, input_items: list[Any], ChatMessage: Any, TextContent: Any, DataContent: Any, Role: Any
|
||||
) -> Any:
|
||||
def _convert_openai_input_to_chat_message(self, input_items: list[Any], ChatMessage: Any, Role: Any) -> Any:
|
||||
"""Convert OpenAI ResponseInputParam to Agent Framework ChatMessage.
|
||||
|
||||
Processes text, images, files, and other content types from OpenAI format
|
||||
@@ -629,14 +627,12 @@ class AgentFrameworkExecutor:
|
||||
Args:
|
||||
input_items: List of OpenAI ResponseInputItemParam objects (dicts or objects)
|
||||
ChatMessage: ChatMessage class for creating chat messages
|
||||
TextContent: TextContent class for text content
|
||||
DataContent: DataContent class for data/media content
|
||||
Role: Role enum for message roles
|
||||
|
||||
Returns:
|
||||
ChatMessage with converted content
|
||||
"""
|
||||
contents = []
|
||||
contents: list[Content] = []
|
||||
|
||||
# Process each input item
|
||||
for item in input_items:
|
||||
@@ -649,7 +645,7 @@ class AgentFrameworkExecutor:
|
||||
|
||||
# Handle both string content and list content
|
||||
if isinstance(message_content, str):
|
||||
contents.append(TextContent(text=message_content))
|
||||
contents.append(Content.from_text(text=message_content))
|
||||
elif isinstance(message_content, list):
|
||||
for content_item in message_content:
|
||||
# Handle dict content items
|
||||
@@ -658,7 +654,7 @@ class AgentFrameworkExecutor:
|
||||
|
||||
if content_type == "input_text":
|
||||
text = content_item.get("text", "")
|
||||
contents.append(TextContent(text=text))
|
||||
contents.append(Content.from_text(text=text))
|
||||
|
||||
elif content_type == "input_image":
|
||||
image_url = content_item.get("image_url", "")
|
||||
@@ -676,7 +672,7 @@ class AgentFrameworkExecutor:
|
||||
media_type = "image/png"
|
||||
else:
|
||||
media_type = "image/png"
|
||||
contents.append(DataContent(uri=image_url, media_type=media_type))
|
||||
contents.append(Content.from_uri(uri=image_url, media_type=media_type))
|
||||
|
||||
elif content_type == "input_file":
|
||||
# Handle file input
|
||||
@@ -710,7 +706,7 @@ class AgentFrameworkExecutor:
|
||||
# Assume file_data is base64, create data URI
|
||||
data_uri = f"data:{media_type};base64,{file_data}"
|
||||
contents.append(
|
||||
DataContent(
|
||||
Content.from_uri(
|
||||
uri=data_uri,
|
||||
media_type=media_type,
|
||||
additional_properties=additional_props,
|
||||
@@ -718,7 +714,7 @@ class AgentFrameworkExecutor:
|
||||
)
|
||||
elif file_url:
|
||||
contents.append(
|
||||
DataContent(
|
||||
Content.from_uri(
|
||||
uri=file_url,
|
||||
media_type=media_type,
|
||||
additional_properties=additional_props,
|
||||
@@ -728,21 +724,19 @@ class AgentFrameworkExecutor:
|
||||
elif content_type == "function_approval_response":
|
||||
# Handle function approval response (DevUI extension)
|
||||
try:
|
||||
from agent_framework import FunctionApprovalResponseContent, FunctionCallContent
|
||||
|
||||
request_id = content_item.get("request_id", "")
|
||||
approved = content_item.get("approved", False)
|
||||
function_call_data = content_item.get("function_call", {})
|
||||
|
||||
# Create FunctionCallContent from the function_call data
|
||||
function_call = FunctionCallContent(
|
||||
function_call = Content.from_function_call(
|
||||
call_id=function_call_data.get("id", ""),
|
||||
name=function_call_data.get("name", ""),
|
||||
arguments=function_call_data.get("arguments", {}),
|
||||
)
|
||||
|
||||
# Create FunctionApprovalResponseContent with correct signature
|
||||
approval_response = FunctionApprovalResponseContent(
|
||||
approval_response = Content.from_function_approval_response(
|
||||
approved, # positional argument
|
||||
id=request_id, # keyword argument 'id', NOT 'request_id'
|
||||
function_call=function_call, # FunctionCallContent object
|
||||
@@ -764,7 +758,7 @@ class AgentFrameworkExecutor:
|
||||
|
||||
# If no contents found, create a simple text message
|
||||
if not contents:
|
||||
contents.append(TextContent(text=""))
|
||||
contents.append(Content.from_text(text=""))
|
||||
|
||||
chat_message = ChatMessage(role=Role.USER, contents=contents)
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ from datetime import datetime
|
||||
from typing import Any, Union
|
||||
from uuid import uuid4
|
||||
|
||||
from agent_framework import ChatMessage, TextContent
|
||||
from agent_framework import ChatMessage, Content
|
||||
from openai.types.responses import (
|
||||
Response,
|
||||
ResponseContentPartAddedEvent,
|
||||
@@ -92,7 +92,7 @@ def _serialize_content_recursive(value: Any) -> Any:
|
||||
if isinstance(value, (list, tuple)):
|
||||
serialized = [_serialize_content_recursive(item) for item in value]
|
||||
# For single-item lists containing text Content, extract just the text
|
||||
# This handles the MCP case where result = [TextContent(text="Hello")]
|
||||
# This handles the MCP case where result = [Content.from_text(text="Hello")]
|
||||
# and we want output = "Hello" not output = '[{"type": "text", "text": "Hello"}]'
|
||||
if len(serialized) == 1 and isinstance(serialized[0], dict) and serialized[0].get("type") == "text":
|
||||
return serialized[0].get("text", "")
|
||||
@@ -127,18 +127,18 @@ class MessageMapper:
|
||||
|
||||
# Register content type mappers for all 12 Agent Framework content types
|
||||
self.content_mappers = {
|
||||
"TextContent": self._map_text_content,
|
||||
"TextReasoningContent": self._map_reasoning_content,
|
||||
"FunctionCallContent": self._map_function_call_content,
|
||||
"FunctionResultContent": self._map_function_result_content,
|
||||
"ErrorContent": self._map_error_content,
|
||||
"UsageContent": self._map_usage_content,
|
||||
"DataContent": self._map_data_content,
|
||||
"UriContent": self._map_uri_content,
|
||||
"HostedFileContent": self._map_hosted_file_content,
|
||||
"HostedVectorStoreContent": self._map_hosted_vector_store_content,
|
||||
"FunctionApprovalRequestContent": self._map_approval_request_content,
|
||||
"FunctionApprovalResponseContent": self._map_approval_response_content,
|
||||
"text": self._map_text_content,
|
||||
"text_reasoning": self._map_reasoning_content,
|
||||
"function_call": self._map_function_call_content,
|
||||
"function_result": self._map_function_result_content,
|
||||
"error": self._map_error_content,
|
||||
"usage": self._map_usage_content,
|
||||
"data": self._map_data_content,
|
||||
"uri": self._map_uri_content,
|
||||
"hosted_file": self._map_hosted_file_content,
|
||||
"hosted_vector_store": self._map_hosted_vector_store_content,
|
||||
"function_approval_request": self._map_approval_request_content,
|
||||
"function_approval_response": self._map_approval_response_content,
|
||||
}
|
||||
|
||||
async def convert_event(self, raw_event: Any, request: AgentFrameworkRequest) -> Sequence[Any]:
|
||||
@@ -603,7 +603,7 @@ class MessageMapper:
|
||||
return events
|
||||
|
||||
# Check if we're streaming text content
|
||||
has_text_content = any(isinstance(content, TextContent) for content in update.contents)
|
||||
has_text_content = any(content.type == "text" for content in update.contents)
|
||||
|
||||
# Check if we're in an executor context with an existing item
|
||||
executor_id = context.get("current_executor_id")
|
||||
@@ -647,10 +647,8 @@ class MessageMapper:
|
||||
|
||||
# Process each content item
|
||||
for content in update.contents:
|
||||
content_type = content.__class__.__name__
|
||||
|
||||
# Special handling for TextContent to use proper delta events
|
||||
if content_type == "TextContent" and "current_message_id" in context:
|
||||
if content.type == "text" and "current_message_id" in context:
|
||||
# Stream text content via proper delta events
|
||||
events.append(
|
||||
ResponseTextDeltaEvent(
|
||||
@@ -663,9 +661,9 @@ class MessageMapper:
|
||||
sequence_number=self._next_sequence(context),
|
||||
)
|
||||
)
|
||||
elif content_type in self.content_mappers:
|
||||
elif content.type in self.content_mappers:
|
||||
# Use existing mappers for other content types
|
||||
mapped_events = await self.content_mappers[content_type](content, context)
|
||||
mapped_events = await self.content_mappers[content.type](content, context)
|
||||
if mapped_events is not None: # Handle None returns (e.g., UsageContent)
|
||||
if isinstance(mapped_events, list):
|
||||
events.extend(mapped_events)
|
||||
@@ -676,7 +674,7 @@ class MessageMapper:
|
||||
events.append(await self._create_unknown_content_event(content, context))
|
||||
|
||||
# Don't increment content_index for text deltas within the same part
|
||||
if content_type != "TextContent":
|
||||
if content.type != "text":
|
||||
context["content_index"] = context.get("content_index", 0) + 1
|
||||
|
||||
except Exception as e:
|
||||
@@ -708,10 +706,8 @@ class MessageMapper:
|
||||
for message in messages:
|
||||
if hasattr(message, "contents") and message.contents:
|
||||
for content in message.contents:
|
||||
content_type = content.__class__.__name__
|
||||
|
||||
if content_type in self.content_mappers:
|
||||
mapped_events = await self.content_mappers[content_type](content, context)
|
||||
if content.type in self.content_mappers:
|
||||
mapped_events = await self.content_mappers[content.type](content, context)
|
||||
if mapped_events is not None: # Handle None returns (e.g., UsageContent)
|
||||
if isinstance(mapped_events, list):
|
||||
events.extend(mapped_events)
|
||||
@@ -726,9 +722,7 @@ class MessageMapper:
|
||||
# Add usage information if present
|
||||
usage_details = getattr(response, "usage_details", None)
|
||||
if usage_details:
|
||||
from agent_framework import UsageContent
|
||||
|
||||
usage_content = UsageContent(details=usage_details)
|
||||
usage_content = Content.from_usage(usage_details=usage_details)
|
||||
await self._map_usage_content(usage_content, context)
|
||||
# Note: _map_usage_content returns None - it accumulates usage for final Response.usage
|
||||
|
||||
@@ -1421,11 +1415,11 @@ class MessageMapper:
|
||||
Returns:
|
||||
None - no event emitted (usage goes in final Response.usage)
|
||||
"""
|
||||
# Extract usage from UsageContent.details (UsageDetails object)
|
||||
details = getattr(content, "details", None)
|
||||
total_tokens = getattr(details, "total_token_count", 0) or 0
|
||||
prompt_tokens = getattr(details, "input_token_count", 0) or 0
|
||||
completion_tokens = getattr(details, "output_token_count", 0) or 0
|
||||
# Extract usage from UsageContent.usage_details (UsageDetails object)
|
||||
details = content.usage_details or {}
|
||||
total_tokens = details.get("total_token_count", 0)
|
||||
prompt_tokens = details.get("input_token_count", 0)
|
||||
completion_tokens = details.get("output_token_count", 0)
|
||||
|
||||
# Accumulate for final Response.usage
|
||||
request_id = context.get("request_id", "default")
|
||||
|
||||
Reference in New Issue
Block a user