Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Python: [Bug]: Public stream close does not release delegated provider responses

オープン
#9,099 コメント 1 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@eavanvalkenburg がすでに取り組んでいます。

2026年10月6日 から。

  • #9100 @quifox による — オープン

評価

この issue はまだ評価されていません。

説明

ag-ui python reproduced
Description

After consuming the first text update, closing a public Python response stream can return before the delegated provider stream has released its HTTP response. The same happens when leaving async with stream after breaking out of iteration.

For AG-UI this reproduces with AGUIChatClient.get_response(..., stream=True) and Agent.run(..., stream=True), both with default function invocation and with function invocation disabled. OpenAI Chat Completions also reproduces through the default function-invocation layer; the direct provider path with that layer disabled closes correctly.

Expected: once await stream.close() or the stream context exit completes, the active delegated response is closed. A caller-supplied shared HTTP client should remain usable.

Actual: the public close completes while the response/body remains open. This is a deterministic cleanup/ownership gap; it does not depend on a live model or garbage collection.

Code Sample

The following uses HTTPX MockTransport and makes no network requests. Keep the response and body referenced, consume only the first chunk, then check cleanup immediately after public close:

import asyncio
import json

import httpx
from agent_framework import Message
from agent_framework_ag_ui import AGUIChatClient


class Body(httpx.AsyncByteStream):
    def __init__(self):
        self.closed = False
        self.chunks_read = 0

    async def __aiter__(self):
        for text in ("first", "later"):
            self.chunks_read += 1
            event = {"type": "TEXT_MESSAGE_CONTENT", "messageId": "m1", "delta": text}
            yield f"data: {json.dumps(event)}\n\n".encode()

    async def aclose(self):
        self.closed = True


async def main():
    body = Body()
    responses = []

    async def handler(request):
        await request.aread()
        response = httpx.Response(
            200, headers={"content-type": "text/event-stream"}, stream=body, request=request
        )
        responses.append(response)
        return response

    async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as http_client:
        client = AGUIChatClient(endpoint="https://agui.example.test/run", http_client=http_client)
        stream = client.get_response([Message(role="user", contents=["Question"])], stream=True)
        try:
            assert (await anext(stream)).text == "first"
            assert body.chunks_read == 1
            await stream.close()
            print({"response_closed": responses[0].is_closed, "body_closed": body.closed})
            assert responses[0].is_closed and body.closed
            assert not http_client.is_closed
        finally:
            await stream.close()
            await responses[0].aclose()
            await client.close()


asyncio.run(main())

On the current implementation, both closure flags are False and the assertion fails. With the forwarding cleanup fixed, both values are True.

Package Versions

Source checkout at b9d24c8fb484c8330abe8bb9e7500ca3c3bbf46c: agent-framework-core 1.20.0 and agent-framework-ag-ui 1.5.0.

Python Version

Python 3.12.12, Linux x86_64.

Additional Context

The provider-specific OpenAI cleanup in #8773 fixed #8762. This report concerns the remaining forwarding layers: function invocation owns an inner ResponseStream, while the AG-UI conversion generator owns the post_run event stream. Neither forwarding scope currently awaits inner closure when the consumer stops early.

Full consumption and directly closing the AG-UI HTTP-service generator pass as controls. Loopback TCP/SSE checks confirm the early-close gap with real HTTPX socket transport as well.

I will submit a focused fix using the existing ResponseStream context manager and awaited AG-UI event-stream closure, with regression tests for public client and agent entry points.

主要言語
Python
スター
13.9k
フォーク
2.4k
平均マージ
1日 16時間
マージ済み PR(30日)
440

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

microsoft/agent-framework のほかの issue

microsoft/agent-framework の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。