From 4c7d1088ba28d4f66690fe94dd4f5981046b07b0 Mon Sep 17 00:00:00 2001 From: mountain Date: Thu, 9 Jul 2026 19:57:43 +0800 Subject: [PATCH] fix: forward stream_metadata to the chat API in legacy client chat_completions(stream=True, stream_metadata=True) used stream_metadata only to pick the raw dict-chunk parser locally, never adding it to the request payload. The wire request didn't match the caller's intent and relied on the server sending metadata chunks unconditionally. Forward the flag (mirroring the modern CloudBackend, which always sends it) so the request is correct and robust if the server ever gates metadata behind it. Verified against the real API that the server currently emits block_metadata regardless, so this is a latent-correctness fix, not a behavior change today. --- pageindex/cloud_api.py | 6 ++++++ tests/test_legacy_sdk_contract.py | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pageindex/cloud_api.py b/pageindex/cloud_api.py index a4b1039..b8f499a 100644 --- a/pageindex/cloud_api.py +++ b/pageindex/cloud_api.py @@ -147,6 +147,12 @@ class LegacyCloudAPI: payload["temperature"] = temperature if enable_citations: payload["enable_citations"] = enable_citations + # Forward stream_metadata so the wire request matches the caller's intent + # (and stays correct if the server ever gates metadata chunks behind it), + # mirroring the modern CloudBackend which always sends it. It only affects + # streaming responses, where it selects the raw dict-chunk parser below. + if stream_metadata: + payload["stream_metadata"] = stream_metadata response = self._request( "POST", diff --git a/tests/test_legacy_sdk_contract.py b/tests/test_legacy_sdk_contract.py index 2471f1c..44a9938 100644 --- a/tests/test_legacy_sdk_contract.py +++ b/tests/test_legacy_sdk_contract.py @@ -238,7 +238,10 @@ def test_chat_completions_stream_metadata_returns_raw_chunks(monkeypatch): )) assert chunks == [{"object": "chat.completion.chunk"}] - assert "stream_metadata" not in calls[0]["json"] + # stream_metadata must be forwarded to the server so the wire request matches + # the caller's intent (and mirrors the modern CloudBackend), not kept as a + # client-only parser switch. + assert calls[0]["json"]["stream_metadata"] is True def test_chat_completions_stream_errors_are_pageindex_api_error(monkeypatch):