Skip to content

[Bug] StreamListener crashes when _output_type is a Generic #9099

@clxyder

Description

@clxyder

What happened?

StreamListener crashes when _output_type is a Generic with the following error message:

TypeError: issubclass() arg 1 must be a class

Steps to reproduce

MNE

import asyncio
import dspy

dspy.configure(lm=dspy.LM("openai/gpt-4o-mini", cache=False))

# Create the program and wrap it with streaming functionality
class Hello(dspy.Signature):
    world: List[str] = dspy.OutputField()

predict = dspy.Predict(Hello)
stream_listeners = [
    dspy.streaming.StreamListener(signature_field_name="world"),
]
stream_predict = dspy.streamify(predict, stream_listeners=stream_listeners)

async def use_streaming():
    output = stream_predict(
        question="why did a chicken cross the kitchen?",
        include_final_prediction_in_output_stream=False,
    )
    return_value = None
    async for value in output:
        if isinstance(value, dspy.Prediction):
            return_value = value
        else:
            print(value)
    return return_value

output = asyncio.run(use_streaming())
print(output)

this happens because issubclass expects a non-generic class.

if self._output_type and issubclass(self._output_type, Type) and self._output_type.is_streamable():

the following snippet shows the core components at work:

import dspy
from typing import List
    
class Hello(dspy.Signature):
    world: List[str] = dspy.OutputField()

t=Hello.output_fields["world"].annotation
print(f"{t=}")

# Only call issubclass if t is actually a class
import inspect
if inspect.isclass(t):
    issubclass(t, str)
else: print("not a class")

it seem that an easy way to fix the issue would be to check if self._output_type is an instantiable class before running issubclass.

i'm bypassing that check entirely by overloading the _output_type property with type(str)

DSPy version

3.1.0b1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions