fix(utils.py): don't send subsequent chunks if last chunk sent

prevents multiple empty finish chunks from being sent
This commit is contained in:
Krrish Dholakia 2024-03-26 13:49:03 -07:00
parent 0f432dfb4b
commit 3a82ff2ef2
2 changed files with 30 additions and 0 deletions

View file

@ -824,6 +824,32 @@ def test_bedrock_claude_3_streaming():
pytest.fail(f"Error occurred: {e}") pytest.fail(f"Error occurred: {e}")
def test_claude_3_streaming_finish_reason():
try:
litellm.set_verbose = True
messages = [
{"role": "system", "content": "Be helpful"},
{"role": "user", "content": "What do you know?"},
]
response: ModelResponse = completion(
model="claude-3-opus-20240229",
messages=messages,
stream=True,
)
complete_response = ""
# Add any assertions here to check the response
num_finish_reason = 0
for idx, chunk in enumerate(response):
if isinstance(chunk, ModelResponse):
if chunk.choices[0].finish_reason is not None:
num_finish_reason += 1
assert num_finish_reason == 1
except RateLimitError:
pass
except Exception as e:
pytest.fail(f"Error occurred: {e}")
@pytest.mark.skip(reason="Replicate changed exceptions") @pytest.mark.skip(reason="Replicate changed exceptions")
def test_completion_replicate_stream_bad_key(): def test_completion_replicate_stream_bad_key():
try: try:

View file

@ -9531,6 +9531,9 @@ class CustomStreamWrapper:
else: else:
return return
elif self.received_finish_reason is not None: elif self.received_finish_reason is not None:
if self.sent_last_chunk == True:
raise StopIteration
# flush any remaining holding chunk # flush any remaining holding chunk
if len(self.holding_chunk) > 0: if len(self.holding_chunk) > 0:
if model_response.choices[0].delta.content is None: if model_response.choices[0].delta.content is None:
@ -9544,6 +9547,7 @@ class CustomStreamWrapper:
is_delta_empty = self.is_delta_empty( is_delta_empty = self.is_delta_empty(
delta=model_response.choices[0].delta delta=model_response.choices[0].delta
) )
if is_delta_empty: if is_delta_empty:
# get any function call arguments # get any function call arguments
model_response.choices[0].finish_reason = map_finish_reason( model_response.choices[0].finish_reason = map_finish_reason(