Skip to content

Fix generate_stream error #1243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
from unittest.mock import MagicMock, patch

import pytest
from huggingface_hub import ChatCompletionOutputMessage
from huggingface_hub import (
ChatCompletionOutputMessage,
ChatCompletionStreamOutput,
ChatCompletionStreamOutputChoice,
ChatCompletionStreamOutputDelta,
)

from smolagents.models import (
AmazonBedrockServerModel,
Expand Down Expand Up @@ -207,6 +212,40 @@ def test_get_hfapi_message_no_tool_external_provider(self):
messages = [{"role": "user", "content": [{"type": "text", "text": "Hello!"}]}]
model(messages, stop_sequences=["great"])

@require_run_all
def test_generate_stream_error(self):
# Setting max_tokens to 5 to get finish_reason='length'
model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", max_tokens=5)
messages = [{"role": "user", "content": [{"type": "text", "text": "Hello!"}]}]
with pytest.raises(ValueError, match="No content or tool calls in event:"):
list(model.generate_stream(messages))

def test_generate_stream_error_with_mock_client(self):
# Setting max_tokens to 5 to get finish_reason='length'
model = InferenceClientModel(model_id="test-model", max_tokens=5)
model.client = MagicMock()
# Mock the response to simulate finish_reason='length'
model.client.chat.completions.create.return_value = [
ChatCompletionStreamOutput(
choices=[
ChatCompletionStreamOutputChoice(
delta=ChatCompletionStreamOutputDelta(role="assistant", content=" I", tool_calls=None),
index=0,
finish_reason="length",
logprobs=None,
)
],
created=1,
id="",
model="test-model",
system_fingerprint="3.2.1-sha-4d28897",
usage=None,
)
]
messages = [{"role": "user", "content": [{"type": "text", "text": "Hello!"}]}]
with pytest.raises(ValueError, match="No content or tool calls in event:"):
list(model.generate_stream(messages))


class TestHfApiModel:
def test_init_model_with_tokens(self):
Expand Down
Loading