Skip to content

[http-client-python] Add automatic SSE reconnection - #11904

Draft
Libba Lawrence (l0lawrence) wants to merge 2 commits into
microsoft:mainfrom
l0lawrence:l0lawrence-python-sse-reconnection
Draft

[http-client-python] Add automatic SSE reconnection#11904
Libba Lawrence (l0lawrence) wants to merge 2 commits into
microsoft:mainfrom
l0lawrence:l0lawrence-python-sse-reconnection

Conversation

@l0lawrence

@l0lawrence Libba Lawrence (l0lawrence) commented Sep 9, 2026

Copy link
Copy Markdown
Member

Structured SSE streams currently stop at unexpected EOF and require callers to manually resume with last_event_id through an internal workaround. This adds automatic reconnection so generated clients follow the SSE processing model without requiring application-owned reconnect loops.

The generated stream runtime now:

  • reconnects after unexpected EOF using the default three-second delay or the latest valid retry: value --- Do we want a 3 second default or would we prefer no time delay, just immediately try to reconnect?
  • preserves and sends the latest event ID through Last-Event-ID
  • stops reconnecting after terminal events and HTTP 204 responses (per the spec)
  • routes reconnect failures through the generated operation's normal error mapping
  • supports both sync and async streams

Closes #11761

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 6bf64cba-786a-41ee-8e45-8567e1cf8568
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:python Issue for the Python client emitter: @typespec/http-client-python label Sep 9, 2026
@pkg-pr-new

pkg-pr-new Bot commented Sep 9, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-python@11904

commit: 01b0bd9

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • @typespec/http-client-python
Show changes

@typespec/http-client-python - feature ✏️

Automatically reconnect SSE streams after an unexpected disconnect, honoring server-provided retry delays and resuming with the latest event ID.,> ,> python,> with client.events.stream() as stream:,> for event in stream:,> print(event),>

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 6bf64cba-786a-41ee-8e45-8567e1cf8568
@azure-sdk-automation

Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

await aclose()
finally:
await self._response.close()
await asyncio.sleep((self._retry if self._retry is not None else 3000) / 1000)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you have access to the transport here you can do await transport.sleep instead of importing asyncio directly. There is a way on core to do it for async

events = self._decoder.aiter_events(self._response.iter_bytes())
try:
async for event in events:
event_id = getattr(event, "id", None)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think metadata-only SSE blocks are lost here. Per the SSE processing model,  id  updates the last event id buffer and retry updates the reconnection time even when the block has no  data field and therefore dispatches no event. Because this state is only copied from yielded events, an EOF immediately after a metadata-only block reconnects with stale values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:python Issue for the Python client emitter: @typespec/http-client-python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[http-client-python] Add SSE reconnection

2 participants