Skip to content

Commit 5e09233

Browse files
committed
test(openai): cover streaming chat completion reassembly
Add tests for the params.stream opt-in: non-streaming default preserved, streaming request flags, chunk reassembly into the classic response shape, streamed tool-call accumulation, stream_options rejection fallback, and call_model end-to-end.
1 parent 138261f commit 5e09233

1 file changed

Lines changed: 197 additions & 0 deletions

File tree

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
"""Tests for OpenAIProvider streaming chat completions (issue #653).
2+
3+
Anthropic-backed OpenAI-compatible endpoints reject non-streaming requests
4+
that run longer than 10 minutes. With ``params.stream: true`` on the model
5+
node, the provider must issue a streaming request and reassemble the chunks
6+
into the classic chat.completion shape the rest of the pipeline expects.
7+
"""
8+
9+
from types import SimpleNamespace
10+
from unittest.mock import MagicMock
11+
12+
import openai
13+
import pytest
14+
15+
from entity.configs import AgentConfig
16+
from entity.messages import Message, MessageRole
17+
from runtime.node.agent.providers.openai_provider import OpenAIProvider
18+
19+
20+
# ---------------------------------------------------------------------------
21+
# Helpers
22+
# ---------------------------------------------------------------------------
23+
24+
def _make_provider(params=None) -> OpenAIProvider:
25+
config = MagicMock(spec=AgentConfig)
26+
config.base_url = None
27+
config.api_key = "test-key"
28+
config.name = "test-model"
29+
config.provider = "openai"
30+
config.params = params or {}
31+
config.token_tracker = None
32+
return OpenAIProvider(config)
33+
34+
35+
def _chunk(content=None, role=None, tool_calls=None, finish_reason=None, usage=None):
36+
delta = SimpleNamespace(role=role, content=content, tool_calls=tool_calls)
37+
choice = SimpleNamespace(index=0, delta=delta, finish_reason=finish_reason)
38+
return SimpleNamespace(
39+
id="chatcmpl-123",
40+
model="test-model",
41+
created=1,
42+
choices=[choice] if (content or role or tool_calls or finish_reason) else [],
43+
usage=usage,
44+
)
45+
46+
47+
def _tool_call_delta(index, call_id=None, name=None, arguments=None):
48+
return SimpleNamespace(
49+
index=index,
50+
id=call_id,
51+
function=SimpleNamespace(name=name, arguments=arguments),
52+
)
53+
54+
55+
def _make_client(chunks):
56+
client = MagicMock()
57+
client.chat.completions.create.return_value = iter(chunks)
58+
return client
59+
60+
61+
# ---------------------------------------------------------------------------
62+
# Non-streaming default is preserved
63+
# ---------------------------------------------------------------------------
64+
65+
def test_default_call_is_non_streaming():
66+
provider = _make_provider()
67+
client = MagicMock()
68+
provider._create_chat_completion(client, {"model": "test-model", "messages": []})
69+
70+
kwargs = client.chat.completions.create.call_args.kwargs
71+
assert "stream" not in kwargs
72+
assert "stream_options" not in kwargs
73+
74+
75+
# ---------------------------------------------------------------------------
76+
# Streaming opt-in
77+
# ---------------------------------------------------------------------------
78+
79+
def test_stream_param_enables_streaming_request():
80+
provider = _make_provider(params={"stream": True})
81+
client = _make_client([_chunk(content="hi", finish_reason="stop")])
82+
provider._create_chat_completion(client, {"model": "test-model", "messages": []})
83+
84+
kwargs = client.chat.completions.create.call_args.kwargs
85+
assert kwargs["stream"] is True
86+
assert kwargs["stream_options"] == {"include_usage": True}
87+
88+
89+
def test_stream_chunks_reassembled_into_classic_shape():
90+
provider = _make_provider(params={"stream": True})
91+
usage = SimpleNamespace(
92+
prompt_tokens=10, completion_tokens=5, total_tokens=15
93+
)
94+
client = _make_client([
95+
_chunk(role="assistant", content="Hello"),
96+
_chunk(content=", "),
97+
_chunk(content="world"),
98+
_chunk(finish_reason="stop"),
99+
_chunk(usage=usage),
100+
])
101+
102+
response = provider._create_chat_completion(
103+
client, {"model": "test-model", "messages": []}
104+
)
105+
106+
assert response.choices[0].message.content == "Hello, world"
107+
assert response.choices[0].finish_reason == "stop"
108+
assert response.choices[0].message.tool_calls is None
109+
assert response.usage.total_tokens == 15
110+
111+
# The reassembled response must satisfy the existing deserializer.
112+
message = provider._deserialize_chat_response(response)
113+
assert message.role is MessageRole.ASSISTANT
114+
assert message.text_content() == "Hello, world"
115+
116+
# And token tracking must read the streamed usage payload.
117+
token_usage = provider.extract_token_usage(response)
118+
assert token_usage.input_tokens == 10
119+
assert token_usage.output_tokens == 5
120+
121+
122+
def test_streamed_tool_calls_are_accumulated():
123+
provider = _make_provider(params={"stream": True})
124+
client = _make_client([
125+
_chunk(tool_calls=[_tool_call_delta(0, call_id="call_1", name="search")]),
126+
_chunk(tool_calls=[_tool_call_delta(0, arguments='{"query": ')]),
127+
_chunk(tool_calls=[_tool_call_delta(0, arguments='"chatdev"}')]),
128+
_chunk(finish_reason="tool_calls"),
129+
])
130+
131+
response = provider._create_chat_completion(
132+
client, {"model": "test-model", "messages": []}
133+
)
134+
135+
calls = response.choices[0].message.tool_calls
136+
assert len(calls) == 1
137+
assert calls[0].id == "call_1"
138+
assert calls[0].function.name == "search"
139+
assert calls[0].function.arguments == '{"query": "chatdev"}'
140+
141+
message = provider._deserialize_chat_response(response)
142+
assert message.tool_calls[0].function_name == "search"
143+
assert message.tool_calls[0].arguments == '{"query": "chatdev"}'
144+
145+
146+
def test_stream_true_in_payload_params_also_streams():
147+
"""`stream` passed through node params ends up in the payload; it must not
148+
reach the SDK as a bare kwarg on the reassembly path."""
149+
provider = _make_provider()
150+
client = _make_client([_chunk(content="ok", finish_reason="stop")])
151+
response = provider._create_chat_completion(
152+
client, {"model": "test-model", "messages": [], "stream": True}
153+
)
154+
assert response.choices[0].message.content == "ok"
155+
156+
157+
def test_stream_options_rejection_falls_back_without_usage():
158+
provider = _make_provider(params={"stream": True})
159+
client = MagicMock()
160+
bad_request = openai.BadRequestError(
161+
message="stream_options not supported",
162+
response=MagicMock(status_code=400, headers={}),
163+
body=None,
164+
)
165+
client.chat.completions.create.side_effect = [
166+
bad_request,
167+
iter([_chunk(content="ok", finish_reason="stop")]),
168+
]
169+
170+
response = provider._create_chat_completion(
171+
client, {"model": "test-model", "messages": []}
172+
)
173+
174+
assert response.choices[0].message.content == "ok"
175+
retry_kwargs = client.chat.completions.create.call_args.kwargs
176+
assert retry_kwargs["stream"] is True
177+
assert "stream_options" not in retry_kwargs
178+
179+
180+
def test_call_model_streams_end_to_end():
181+
provider = _make_provider(params={"stream": True, "protocol": "chat"})
182+
client = _make_client([
183+
_chunk(role="assistant", content="done"),
184+
_chunk(finish_reason="stop"),
185+
])
186+
187+
timeline = []
188+
result = provider.call_model(
189+
client,
190+
conversation=[Message(role=MessageRole.USER, content="go")],
191+
timeline=timeline,
192+
)
193+
194+
assert result.message.text_content() == "done"
195+
assert timeline[-1]["content"] == "done"
196+
kwargs = client.chat.completions.create.call_args.kwargs
197+
assert kwargs["stream"] is True

0 commit comments

Comments
 (0)