Skip to content

Commit ee294c2

Browse files
author
kevin
committed
refactor: migrate incremental_to_full flag from headers to user_agent parameter
1 parent 5070eed commit ee294c2

7 files changed

Lines changed: 53 additions & 33 deletions

File tree

dashscope/aigc/generation.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,13 @@ def call( # pylint: disable=arguments-renamed,too-many-branches,too-many-statem
179179
to_merge_incremental_output = True
180180
parameters["incremental_output"] = True
181181

182-
# Pass incremental_to_full flag via headers user-agent
183-
if "headers" not in parameters:
184-
parameters["headers"] = {}
182+
# Pass incremental_to_full flag via user_agent parameter
185183
flag = "1" if to_merge_incremental_output else "0"
186-
parameters["headers"]["user-agent"] = f"incremental_to_full/{flag}"
184+
existing_ua = parameters.get("user_agent", "")
185+
new_ua = f"incremental_to_full/{flag}"
186+
parameters["user_agent"] = (
187+
f"{existing_ua}; {new_ua}".strip() if existing_ua else new_ua
188+
)
187189

188190
response = super().call(
189191
model=model,
@@ -434,11 +436,13 @@ async def call( # type: ignore[override] # pylint: disable=arguments-renamed,to
434436
to_merge_incremental_output = True
435437
parameters["incremental_output"] = True
436438

437-
# Pass incremental_to_full flag via headers user-agent
438-
if "headers" not in parameters:
439-
parameters["headers"] = {}
439+
# Pass incremental_to_full flag via user_agent parameter
440440
flag = "1" if to_merge_incremental_output else "0"
441-
parameters["headers"]["user-agent"] = f"incremental_to_full/{flag}"
441+
existing_ua = parameters.get("user_agent", "")
442+
new_ua = f"incremental_to_full/{flag}"
443+
parameters["user_agent"] = (
444+
f"{existing_ua}; {new_ua}".strip() if existing_ua else new_ua
445+
)
442446

443447
response = await super().call(
444448
model=model,

dashscope/aigc/image_generation.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,13 @@ def call( # type: ignore[override]
9999
to_merge_incremental_output = True
100100
kwargs["incremental_output"] = True
101101

102-
# Pass incremental_to_full flag via headers user-agent
103-
if "headers" not in kwargs:
104-
kwargs["headers"] = {}
105-
102+
# Pass incremental_to_full flag via user_agent parameter
106103
flag = "1" if to_merge_incremental_output else "0"
107-
kwargs["headers"]["user-agent"] = f"incremental_to_full/{flag}"
104+
existing_ua = kwargs.get("user_agent", "")
105+
new_ua = f"incremental_to_full/{flag}"
106+
kwargs["user_agent"] = (
107+
f"{existing_ua}; {new_ua}".strip() if existing_ua else new_ua
108+
)
108109
if kwargs.get("is_async", False):
109110
kwargs["headers"]["X-DashScope-Async"] = "enable"
110111
task = cls.async_task
@@ -416,12 +417,13 @@ async def call( # type: ignore[override]
416417
to_merge_incremental_output = True
417418
kwargs["incremental_output"] = True
418419

419-
# Pass incremental_to_full flag via headers user-agent
420-
if "headers" not in kwargs:
421-
kwargs["headers"] = {}
422-
420+
# Pass incremental_to_full flag via user_agent parameter
423421
flag = "1" if to_merge_incremental_output else "0"
424-
kwargs["headers"]["user-agent"] = f"incremental_to_full/{flag}"
422+
existing_ua = kwargs.get("user_agent", "")
423+
new_ua = f"incremental_to_full/{flag}"
424+
kwargs["user_agent"] = (
425+
f"{existing_ua}; {new_ua}".strip() if existing_ua else new_ua
426+
)
425427
if kwargs.get("is_async", False):
426428
kwargs["headers"]["X-DashScope-Async"] = "enable"
427429
task = cls.async_task

dashscope/aigc/multimodal_conversation.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,13 @@ def call( # pylint: disable=arguments-renamed,too-many-branches,too-many-statem
163163
to_merge_incremental_output = True
164164
kwargs["incremental_output"] = True
165165

166-
# Pass incremental_to_full flag via headers user-agent
167-
if "headers" not in kwargs:
168-
kwargs["headers"] = {}
166+
# Pass incremental_to_full flag via user_agent parameter
169167
flag = "1" if to_merge_incremental_output else "0"
170-
kwargs["headers"]["user-agent"] = f"incremental_to_full/{flag}"
168+
existing_ua = kwargs.get("user_agent", "")
169+
new_ua = f"incremental_to_full/{flag}"
170+
kwargs["user_agent"] = (
171+
f"{existing_ua}; {new_ua}".strip() if existing_ua else new_ua
172+
)
171173

172174
response = super().call(
173175
model=model,

dashscope/app/application.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,13 @@ def call( # type: ignore[override]
160160
if to_merge_incremental_output:
161161
kwargs["incremental_output"] = True
162162

163-
# Pass incremental_to_full flag via user-agent (append)
164-
if "headers" not in kwargs:
165-
kwargs["headers"] = {}
163+
# Pass incremental_to_full flag via user_agent parameter to avoid
164+
# overwriting the default SDK user-agent
166165
flag = "1" if to_merge_incremental_output else "0"
167-
existing_ua = kwargs["headers"].get("user-agent", "")
166+
existing_ua = kwargs.get("user_agent", "")
168167
new_ua = f"incremental_to_full/{flag}"
169-
kwargs["headers"]["user-agent"] = (
170-
f"{existing_ua} {new_ua}".strip() if existing_ua else new_ua
168+
kwargs["user_agent"] = (
169+
f"{existing_ua}; {new_ua}".strip() if existing_ua else new_ua
171170
)
172171

173172
(
@@ -273,6 +272,11 @@ def _merge_application_response(cls, response):
273272

274273
for rsp in response:
275274
parsed_response = ApplicationResponse.from_api_response(rsp)
275+
if parsed_response.output and not hasattr(
276+
parsed_response.output,
277+
"choices",
278+
):
279+
parsed_response.output.choices = None
276280
result = merge_single_response(
277281
parsed_response,
278282
accumulated_data,

dashscope/audio/qwen_omni/omni_realtime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ def end_session(self, timeout: int = 20) -> None:
371371
# if the server returned an error or timed out, close the connection
372372
if error is not None:
373373
self.close()
374-
return
374+
raise RuntimeError(f"Session ended with error: {error}")
375375
if not finish_success:
376376
self.close()
377377
raise TimeoutError(

dashscope/tokenizers/qwen_tokenizer.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,19 @@ def _split_text(text: str, chunk_size: int = _CHUNK_SIZE) -> List[str]:
138138
parts.append(piece[j : j + chunk_size])
139139

140140
chunks: List[str] = []
141+
current_chunk: List[str] = []
142+
current_len = 0
141143
for part in parts:
142-
if chunks and len(chunks[-1]) + len(part) <= chunk_size:
143-
chunks[-1] += part
144+
if current_len + len(part) <= chunk_size:
145+
current_chunk.append(part)
146+
current_len += len(part)
144147
else:
145-
chunks.append(part)
148+
if current_chunk:
149+
chunks.append("".join(current_chunk))
150+
current_chunk = [part]
151+
current_len = len(part)
152+
if current_chunk:
153+
chunks.append("".join(current_chunk))
146154
return chunks
147155

148156
def decode(

tests/unit/test_conversation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,5 @@ def test_not_qwen(self, mock_server: MockServer):
234234
assert response.output.finish_reason == "stop"
235235
req = mock_server.requests.get(block=True)
236236
assert req["model"] == Generation.Models.dolly_12b_v2
237-
assert req["parameters"] == {}
237+
assert "user_agent" in req["parameters"]
238238
assert req["input"] == {"prompt": prompt}

0 commit comments

Comments
 (0)