Python: [Bug]: Public stream close does not release delegated provider responses
I maintainer di solito rispondono entro 1 giorno
Valutazione
Questa issue non è ancora stata valutata.
Descrizione
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
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di microsoft/agent-framework
-
.NET python triage
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
microsoft/agent-framework#9092 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Python: raw-data content mappings lose annotations and attachment metadataForse già presa @moonbox3 l’ha presa 8 giorni fa. Apertapython triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
microsoft/agent-framework#8632 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Python: Clarify when to use platformForse già presa @eavanvalkenburg l’ha presa 7 giorni fa. Apertapython triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
microsoft/agent-framework#8599 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
.NET Compaction - Update docs to refer to `AIContextProvider` deep diveForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta.NET compaction documentation
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
microsoft/agent-framework#4629 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Python: [Feature]: A deterministic pre-execution verification middleware for agent actions — AgentDojo v2.2 re-run: ASR=0 / FP=0 (open artifacts, full fix-cycle trajectory inside)Forse già presa @eavanvalkenburg l’ha presa 1 giorno fa. Apertaagents middleware python
microsoft/agent-framework#9132 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di microsoft/agent-framework
Issue simili
-
changelog investigate
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
ramnes/notion-sdk-py#409 ·
-
good first issue help wanted
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
lindicaphxag-tech/kaggle#28 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
BSData/horus-heresy-3rd-edition#3211 ·
I maintainer di solito rispondono entro 1 giorno
-
bug needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 1 giorno
-
bug tests
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno