Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implement exceptions for chunk streams also
  • Loading branch information
plpxsk authored and RobinPicard committed Mar 26, 2026
commit 6c1dcb6b6e4d7b554f8b98c8f503e271ef8fd061
17 changes: 11 additions & 6 deletions outlines/models/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,17 @@ def generate_stream(
raise
raise normalize_provider_exception(e, PROVIDER) from e

for chunk in stream:
if (
chunk.type == "content_block_delta"
and chunk.delta.type == "text_delta"
):
yield chunk.delta.text
try:
for chunk in stream:
if (
chunk.type == "content_block_delta"
and chunk.delta.type == "text_delta"
):
yield chunk.delta.text
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e


def from_anthropic(
Expand Down
11 changes: 8 additions & 3 deletions outlines/models/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,14 @@ def generate_stream(
raise
raise normalize_provider_exception(e, PROVIDER) from e

for chunk in stream:
if hasattr(chunk, "text") and chunk.text:
yield chunk.text
try:
for chunk in stream:
if hasattr(chunk, "text") and chunk.text:
yield chunk.text
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e


def from_gemini(client: "Client", model_name: Optional[str] = None) -> Gemini:
Expand Down
42 changes: 26 additions & 16 deletions outlines/models/mistral.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,18 @@ def generate_stream(
raise
raise normalize_provider_exception(e, PROVIDER) from e

for chunk in stream:
if (
hasattr(chunk, "data")
and chunk.data.choices
and chunk.data.choices[0].delta.content is not None
):
yield chunk.data.choices[0].delta.content
try:
for chunk in stream:
if (
hasattr(chunk, "data")
and chunk.data.choices
and chunk.data.choices[0].delta.content is not None
):
yield chunk.data.choices[0].delta.content
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e


class AsyncMistral(AsyncModel):
Expand Down Expand Up @@ -545,15 +550,20 @@ async def generate_stream(
raise
raise normalize_provider_exception(e, PROVIDER) from e

async for chunk in response:
if (
hasattr(chunk, "data")
and chunk.data.choices
and len(chunk.data.choices) > 0
and hasattr(chunk.data.choices[0], "delta")
and chunk.data.choices[0].delta.content is not None
):
yield chunk.data.choices[0].delta.content
try:
async for chunk in response:
if (
hasattr(chunk, "data")
and chunk.data.choices
and len(chunk.data.choices) > 0
and hasattr(chunk.data.choices[0], "delta")
and chunk.data.choices[0].delta.content is not None
):
yield chunk.data.choices[0].delta.content
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e


def from_mistral(
Expand Down
18 changes: 14 additions & 4 deletions outlines/models/ollama.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,13 @@ def generate_stream(
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e
for chunk in response:
yield chunk.message.content
try:
for chunk in response:
yield chunk.message.content
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e


class AsyncOllama(AsyncModel):
Expand Down Expand Up @@ -376,8 +381,13 @@ async def generate_stream( # type: ignore
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e
async for chunk in stream:
yield chunk.message.content
try:
async for chunk in stream:
yield chunk.message.content
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e


def from_ollama(
Expand Down
22 changes: 16 additions & 6 deletions outlines/models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,14 @@ def generate_stream(
raise
raise normalize_provider_exception(e, PROVIDER) from e

for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
try:
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e


class AsyncOpenAI(AsyncModel):
Expand Down Expand Up @@ -516,9 +521,14 @@ async def generate_stream( # type: ignore
raise
raise normalize_provider_exception(e, PROVIDER) from e

async for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
try:
async for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e


def from_openai(
Expand Down
22 changes: 16 additions & 6 deletions outlines/models/sglang.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,14 @@ def generate_stream(
raise
raise normalize_provider_exception(e, PROVIDER) from e

for chunk in stream: # pragma: no cover
if chunk.choices and chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
try:
for chunk in stream: # pragma: no cover
if chunk.choices and chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e

def _build_client_args(
self,
Expand Down Expand Up @@ -358,9 +363,14 @@ async def generate_stream( # type: ignore
raise
raise normalize_provider_exception(e, PROVIDER) from e

async for chunk in stream: # pragma: no cover
if chunk.choices and chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
try:
async for chunk in stream: # pragma: no cover
if chunk.choices and chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e

def _build_client_args(
self,
Expand Down
18 changes: 14 additions & 4 deletions outlines/models/tgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,13 @@ def generate_stream(
raise
raise normalize_provider_exception(e, PROVIDER) from e

for chunk in stream: # pragma: no cover
yield chunk
try:
for chunk in stream: # pragma: no cover
yield chunk
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e

def _build_client_args(
self,
Expand Down Expand Up @@ -320,8 +325,13 @@ async def generate_stream( # type: ignore
raise
raise normalize_provider_exception(e, PROVIDER) from e

async for chunk in stream: # pragma: no cover
yield chunk
try:
async for chunk in stream: # pragma: no cover
yield chunk
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e

def _build_client_args(
self,
Expand Down
22 changes: 16 additions & 6 deletions outlines/models/vllm.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,14 @@ def generate_stream(
raise
raise normalize_provider_exception(e, PROVIDER) from e

for chunk in stream: # pragma: no cover
if chunk.choices and chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
try:
for chunk in stream: # pragma: no cover
if chunk.choices and chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e

def _build_client_args(
self,
Expand Down Expand Up @@ -339,9 +344,14 @@ async def generate_stream( # type: ignore
raise
raise normalize_provider_exception(e, PROVIDER) from e

async for chunk in stream: # pragma: no cover
if chunk.choices and chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
try:
async for chunk in stream: # pragma: no cover
if chunk.choices and chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
except Exception as e:
if not is_provider_exception(e, PROVIDER):
raise
raise normalize_provider_exception(e, PROVIDER) from e

def _build_client_args(
self,
Expand Down