Container for streaming response data. Pydantic model for type-safe streaming responses.
| 42 | |
| 43 | |
| 44 | class StreamingResponse(BaseModel): |
| 45 | """Container for streaming response data. |
| 46 | |
| 47 | Pydantic model for type-safe streaming responses. |
| 48 | """ |
| 49 | |
| 50 | content: str = Field(description="Response content") |
| 51 | tool_calls: list[dict[str, Any]] = Field( |
| 52 | default_factory=list, description="Tool calls from the model" |
| 53 | ) |
| 54 | chunks_received: int = Field(default=0, description="Number of chunks processed") |
| 55 | elapsed_time: float = Field(description="Time taken for the response") |
| 56 | interrupted: bool = Field( |
| 57 | default=False, description="Whether streaming was interrupted" |
| 58 | ) |
| 59 | reasoning_content: str | None = Field( |
| 60 | default=None, description="Reasoning content (for DeepSeek reasoner)" |
| 61 | ) |
| 62 | streaming: bool = Field( |
| 63 | default=True, description="Whether this was a streaming response" |
| 64 | ) |
| 65 | usage: dict[str, int] | None = Field( |
| 66 | default=None, |
| 67 | description="Token usage from provider (input_tokens, output_tokens)", |
| 68 | ) |
| 69 | |
| 70 | model_config = {"frozen": False} |
| 71 | |
| 72 | def to_dict(self) -> dict[str, Any]: |
| 73 | """Convert to dict for backwards compatibility using enums.""" |
| 74 | return { |
| 75 | StreamingResponseField.RESPONSE: self.content, |
| 76 | StreamingResponseField.TOOL_CALLS: self.tool_calls, |
| 77 | StreamingResponseField.CHUNKS_RECEIVED: self.chunks_received, |
| 78 | StreamingResponseField.ELAPSED_TIME: self.elapsed_time, |
| 79 | StreamingResponseField.STREAMING: self.streaming, |
| 80 | StreamingResponseField.INTERRUPTED: self.interrupted, |
| 81 | StreamingResponseField.REASONING_CONTENT: self.reasoning_content, |
| 82 | StreamingResponseField.USAGE: self.usage, |
| 83 | } |
| 84 | |
| 85 | |
| 86 | class ToolCallAccumulator: |
no outgoing calls