mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-10 22:41:41 +00:00
Merge pull request #14965 from Maximgitman/issue-14962-anthropic-streaming-id
Fix Anthropic streaming IDs (#14962)
This commit is contained in:
commit
0350a44ab1
2 changed files with 26 additions and 1 deletions
|
|
@ -51,6 +51,7 @@ from litellm.types.utils import (
|
|||
ModelResponseStream,
|
||||
StreamingChoices,
|
||||
Usage,
|
||||
_generate_id,
|
||||
)
|
||||
|
||||
from ...base import BaseLLM
|
||||
|
|
@ -490,6 +491,8 @@ class ModelResponseIterator:
|
|||
self.content_blocks: List[ContentBlockDelta] = []
|
||||
self.tool_index = -1
|
||||
self.json_mode = json_mode
|
||||
# Generate response ID once per stream to match OpenAI-compatible behavior
|
||||
self.response_id = _generate_id()
|
||||
|
||||
# Track if we're currently streaming a response_format tool
|
||||
self.is_response_format_tool: bool = False
|
||||
|
|
@ -765,6 +768,7 @@ class ModelResponseIterator:
|
|||
)
|
||||
],
|
||||
usage=usage,
|
||||
id=self.response_id,
|
||||
)
|
||||
|
||||
return returned_chunk
|
||||
|
|
@ -936,4 +940,4 @@ class ModelResponseIterator:
|
|||
data_json = json.loads(str_line[5:])
|
||||
return self.chunk_parser(chunk=data_json)
|
||||
else:
|
||||
return ModelResponseStream()
|
||||
return ModelResponseStream(id=self.response_id)
|
||||
|
|
|
|||
|
|
@ -439,3 +439,24 @@ def test_multiple_tools_streaming_has_index_zero():
|
|||
assert (
|
||||
parsed.choices[0].index == 0
|
||||
), f"Expected index=0, got {parsed.choices[0].index}"
|
||||
|
||||
|
||||
def test_streaming_chunks_have_stable_ids():
|
||||
iterator = ModelResponseIterator(
|
||||
streaming_response=MagicMock(), sync_stream=False, json_mode=False
|
||||
)
|
||||
first_chunk = {
|
||||
"type": "content_block_delta",
|
||||
"index": 0,
|
||||
"delta": {"type": "text_delta", "text": "Hello"},
|
||||
}
|
||||
second_chunk = {
|
||||
"type": "content_block_delta",
|
||||
"index": 0,
|
||||
"delta": {"type": "text_delta", "text": " world"},
|
||||
}
|
||||
|
||||
response_one = iterator.chunk_parser(chunk=first_chunk)
|
||||
response_two = iterator.chunk_parser(chunk=second_chunk)
|
||||
|
||||
assert response_one.id == response_two.id == iterator.response_id
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue