Skip to content

Support Streaming of Function Call Arguments #834

Open
@devtalker

Description

@devtalker

Problem Description

Currently, the stream_events() method in OpenAI Agents Python SDK does not support real-time streaming of function call arguments. While text content can be streamed token-by-token using ResponseTextDeltaEvent, function call arguments are only emitted after the entire function call is complete.

Looking at the current implementation in src/agents/models/chatcmpl_stream_handler.py, function call data is accumulated during streaming and only sent as events after the stream ends:

# Current behavior: accumulate and send at the end
if delta.tool_calls:
    for tc_delta in delta.tool_calls:
        # ... accumulate arguments ...
        state.function_calls[tc_delta.index].arguments += (
            tc_function.arguments if tc_function else ""
        ) or ""

Use Case and Impact on User Experience

This limitation significantly impacts user experience in scenarios where function calls involve large parameters. Here's a concrete example:

Code Agent with File MCP Integration

Consider a coding assistant agent that uses Model Context Protocol (MCP) file operations to write code files. When a user requests:

"Please create a React component for a user dashboard with authentication, data tables, and responsive design"

The agent needs to call a write_file function with the complete code as a parameter:

{
  "function": "write_file",
  "arguments": {
    "path": "src/components/UserDashboard.tsx",
    "content": "import React, { useState, useEffect } from 'react';\nimport { AuthProvider } from '../auth/AuthProvider';\nimport { DataTable } from '../components/DataTable';\n\n// ... hundreds of lines of React component code ..."
  }
}

Current Poor User Experience

Without function call argument streaming:

  • User sees no progress for potentially 10-30 seconds while the agent generates code
  • No indication that the agent is actively writing the file content
  • Appears as if the system is frozen or unresponsive
  • No way to preview the code being generated in real-time

Expected Better User Experience

With function call argument streaming:

  • User sees code being generated line by line: "import React..." → "useState..." → "export default..."
  • Real-time feedback that the agent is actively working
  • Ability to understand what the agent is creating as it happens
  • Much more engaging and transparent interaction

Additional Use Cases

This feature would benefit many other scenarios:

  • API request generation: Large JSON payloads for complex API calls
  • Database queries: Complex SQL queries with extensive WHERE clauses
  • Configuration files: YAML/JSON configuration generation
  • Documentation: Markdown content generation through tools
  • Data transformation: Large data processing function parameters

Current Behavior vs Expected Behavior

Current: Function call arguments appear all at once after completion
Expected: Function call arguments stream incrementally as they are generated

This is especially important for maintaining user engagement and providing transparent feedback during long-running function call generations.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions