Skip to content
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

Raise error if encountered in chat completion SSE stream #2558

Merged
merged 1 commit into from
Sep 20, 2024

Conversation

Wauplin
Copy link
Contributor

@Wauplin Wauplin commented Sep 20, 2024

Fix #2514 cc @lhoestq @OlivierDehaene

When stream=True is passed, the server response can be HTTP 200 but still get an error as a stream event. We were already parsing them for text_generation task, but not chat_completion. This PR fixes this.

Example

from huggingface_hub import InferenceClient


client = InferenceClient("microsoft/Phi-3-mini-4k-instruct")

for message in client.chat_completion(
    messages=[{"role": "user", "content": "Hello there !"}],
    stream=True,
    max_tokens=4091,  # values lower or equal to 4090 work
):
    print(message.choices[0].delta.content, end="")
Traceback (most recent call last):
  File "/home/wauplin/projects/huggingface_hub/idefi.py", line 6, in <module>
    for message in client.chat_completion(
  File "/home/wauplin/projects/huggingface_hub/src/huggingface_hub/inference/_common.py", line 321, in _stream_chat_completion_response
    output = _format_chat_completion_stream_output(item)
  File "/home/wauplin/projects/huggingface_hub/src/huggingface_hub/inference/_common.py", line 356, in _format_chat_completion_stream_output
    raise _parse_text_generation_error(json_payload["error"], json_payload.get("error_type"))
huggingface_hub.errors.ValidationError: Input validation error: `inputs` tokens + `max_new_tokens` must be <= 4096. Given: 6 `inputs` tokens and 4091 `max_new_tokens`

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Contributor

@hanouticelina hanouticelina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@Wauplin
Copy link
Contributor Author

Wauplin commented Sep 20, 2024

Thanks for the review!

@Wauplin Wauplin merged commit c0fd4e0 into main Sep 20, 2024
19 checks passed
@Wauplin Wauplin deleted the 2514-raise-error-if-SSE-error branch September 20, 2024 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

InferenceClient: TypeError: 'NoneType' object is not subscriptable if max_tokens is too big
3 participants