Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta
#9,099 1 commento 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@eavanvalkenburg ci sta già lavorando.

Dal 6/10/2026.

  • #9100 di @quifox — aperta

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

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.

Lingua principale
Python
Stelle
13.9k
Fork
2.4k
Merge medio
1g 16h
PR unite (30g)
440

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/agent-framework

Tutte le issue di microsoft/agent-framework

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.