Skip to content
31 changes: 19 additions & 12 deletions temporalio/contrib/openai_agents/_temporal_model_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

logger = logging.getLogger(__name__)

from typing import Any, AsyncIterator, Sequence, Union, cast
from typing import Any, AsyncIterator, Union, cast

from agents import (
AgentOutputSchema,
Expand Down Expand Up @@ -54,7 +54,7 @@ def __init__(
async def get_response(
self,
system_instructions: Optional[str],
input: Union[str, list[TResponseInputItem], dict[str, str]],
input: Union[str, list[TResponseInputItem]],
model_settings: ModelSettings,
tools: list[Tool],
output_schema: Optional[AgentOutputSchemaBase],
Expand All @@ -65,23 +65,30 @@ async def get_response(
prompt: Optional[ResponsePromptParam],
) -> ModelResponse:
def get_summary(
input: Union[str, list[TResponseInputItem], dict[str, str]],
input: Union[str, list[TResponseInputItem]],
) -> str:
### Activity summary shown in the UI
try:
max_size = 100
if isinstance(input, str):
return input[:max_size]
elif isinstance(input, list):
seq_input = cast(Sequence[Any], input)
last_item = seq_input[-1]
if isinstance(last_item, dict):
return last_item.get("content", "")[:max_size]
elif hasattr(last_item, "content"):
return str(getattr(last_item, "content"))[:max_size]
return str(last_item)[:max_size]
elif isinstance(input, dict):
return input.get("content", "")[:max_size]
content: Any = [
item
for item in input
if (item.get("type") or "message") == "message"
][-1]
if isinstance(content, dict):
content = content.get("content", "")
elif hasattr(content, "content"):
content = getattr(content, "content")

if isinstance(content, list):
content = content[-1]

if isinstance(content, dict) and content.get("text") is not None:
content = content.get("text")
return str(content)[:max_size]
except Exception as e:
logger.error(f"Error getting summary: {e}")
return ""
Expand Down
2 changes: 1 addition & 1 deletion temporalio/contrib/openai_agents/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async def run_activity(ctx: RunContextWrapper[Any], input: str) -> Any:
cancellation_type=cancellation_type,
activity_id=activity_id,
versioning_intent=versioning_intent,
summary=summary,
summary=summary or schema.description,
priority=priority,
)
try:
Expand Down
3 changes: 2 additions & 1 deletion tests/contrib/openai_agents/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,8 @@ async def test_research_workflow(client: Client, use_local_model: bool):
new_config["plugins"] = [
openai_agents.OpenAIAgentsPlugin(
model_params=ModelActivityParameters(
start_to_close_timeout=timedelta(seconds=30)
start_to_close_timeout=timedelta(seconds=120),
schedule_to_close_timeout=timedelta(seconds=120),
),
model_provider=TestModelProvider(TestResearchModel())
if use_local_model
Expand Down
Loading