Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions agent-openai-agents-sdk/agent_server/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
)

from agent_server.utils import (
build_mcp_url,
get_user_workspace_client,
process_agent_stream_events, build_mcp_url,
process_agent_stream_events,
sanitize_output_items,
)

# NOTE: this will work for all databricks models OTHER than GPT-OSS, which uses a slightly different API
Expand Down Expand Up @@ -48,7 +50,7 @@ async def invoke(request: ResponsesAgentRequest) -> ResponsesAgentResponse:
agent = create_coding_agent(mcp_server)
messages = [i.model_dump() for i in request.input]
result = await Runner.run(agent, messages)
return ResponsesAgentResponse(output=[item.to_input_item() for item in result.new_items])
return ResponsesAgentResponse(output=sanitize_output_items(result.new_items))


@stream()
Expand Down
26 changes: 25 additions & 1 deletion agent-openai-agents-sdk/agent_server/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
from typing import AsyncGenerator, AsyncIterator, Optional
from uuid import uuid4
Expand Down Expand Up @@ -29,6 +30,29 @@ def get_user_workspace_client() -> WorkspaceClient:
return WorkspaceClient(token=token, auth_type="pat")


def _sanitize_item(input_item: dict) -> dict:
Comment thread
bbqiu marked this conversation as resolved.
"""Sanitize a single output item dict for Pydantic validation.

MCP tool calls (e.g. Genie) can return items where the ``output`` field is
a *list* of content objects instead of a plain string. MLflow's Pydantic
models expect ``output`` to be a string, so this serialises any non-string
Comment thread
bbqiu marked this conversation as resolved.
values to JSON.

TODO: Remove once https://github.com/mlflow/mlflow/pull/20777 is released.
"""
if not isinstance(input_item.get("output"), str):
try:
input_item["output"] = json.dumps(input_item.get("output"))
except (TypeError, ValueError):
input_item["output"] = str(input_item.get("output"))
return input_item


def sanitize_output_items(items) -> list[dict]:
Comment thread
bbqiu marked this conversation as resolved.
"""Convert agent output items to dicts safe for ResponsesAgentResponse."""
return [_sanitize_item(item.to_input_item()) for item in items]


async def process_agent_stream_events(
async_stream: AsyncIterator[StreamEvent],
) -> AsyncGenerator[ResponsesAgentStreamEvent, None]:
Expand All @@ -47,5 +71,5 @@ async def process_agent_stream_events(
elif event.type == "run_item_stream_event" and event.item.type == "tool_call_output_item":
yield ResponsesAgentStreamEvent(
type="response.output_item.done",
item=event.item.to_input_item(),
item=_sanitize_item(event.item.to_input_item()),
)