|
| 1 | +""" |
| 2 | +Deprecated streaming functionality for backwards compatibility with v1 SDK. |
| 3 | +
|
| 4 | +This module provides the stream() function which wraps replicate.use() with streaming=True. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import warnings |
| 10 | +from typing import Any, Dict, Type, Union, Iterator, AsyncIterator, overload |
| 11 | +from typing_extensions import deprecated |
| 12 | + |
| 13 | +from .._client import Client, AsyncClient |
| 14 | +from ._predictions_use import use |
| 15 | + |
| 16 | +__all__ = ["stream"] |
| 17 | + |
| 18 | + |
| 19 | +def _format_deprecation_message(ref: str, input: Dict[str, Any]) -> str: |
| 20 | + """Format the deprecation message with a working example.""" |
| 21 | + # Format the input dict for display |
| 22 | + input_str = "{\n" |
| 23 | + for key, value in input.items(): |
| 24 | + if isinstance(value, str): |
| 25 | + input_str += f' "{key}": "{value}",\n' |
| 26 | + else: |
| 27 | + input_str += f' "{key}": {value},\n' |
| 28 | + input_str += " }" |
| 29 | + |
| 30 | + return ( |
| 31 | + f"replicate.stream() is deprecated and will be removed in a future version. " |
| 32 | + f"Use replicate.use() with streaming=True instead:\n\n" |
| 33 | + f' model = replicate.use("{ref}", streaming=True)\n' |
| 34 | + f" for event in model(input={input_str}):\n" |
| 35 | + f' print(str(event), end="")\n' |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +@overload |
| 40 | +def stream( |
| 41 | + client: Type[Client], |
| 42 | + ref: str, |
| 43 | + *, |
| 44 | + input: Dict[str, Any], |
| 45 | +) -> Iterator[str]: ... |
| 46 | + |
| 47 | + |
| 48 | +@overload |
| 49 | +def stream( |
| 50 | + client: Type[AsyncClient], |
| 51 | + ref: str, |
| 52 | + *, |
| 53 | + input: Dict[str, Any], |
| 54 | +) -> AsyncIterator[str]: ... |
| 55 | + |
| 56 | + |
| 57 | +@deprecated("replicate.stream() is deprecated. Use replicate.use() with streaming=True instead") |
| 58 | +def stream( |
| 59 | + client: Union[Type[Client], Type[AsyncClient]], |
| 60 | + ref: str, |
| 61 | + *, |
| 62 | + input: Dict[str, Any], |
| 63 | +) -> Union[Iterator[str], AsyncIterator[str]]: |
| 64 | + """ |
| 65 | + Run a model and stream its output (deprecated). |
| 66 | +
|
| 67 | + This function is deprecated. Use replicate.use() with streaming=True instead: |
| 68 | +
|
| 69 | + model = replicate.use("anthropic/claude-4.5-sonnet", streaming=True) |
| 70 | + for event in model(input={"prompt": "Hello"}): |
| 71 | + print(str(event), end="") |
| 72 | +
|
| 73 | + Args: |
| 74 | + client: The Replicate client class (Client or AsyncClient) |
| 75 | + ref: Reference to the model to run. Can be a string with owner/name format |
| 76 | + (e.g., "anthropic/claude-4.5-sonnet") or owner/name:version format. |
| 77 | + input: Dictionary of input parameters for the model. The required keys depend |
| 78 | + on the specific model being run. |
| 79 | +
|
| 80 | + Returns: |
| 81 | + An iterator (or async iterator) that yields output strings as they are |
| 82 | + generated by the model |
| 83 | +
|
| 84 | + Raises: |
| 85 | + DeprecationWarning: Always raised when this function is called |
| 86 | + """ |
| 87 | + # Log deprecation warning with helpful migration example |
| 88 | + warnings.warn( |
| 89 | + _format_deprecation_message(ref, input), |
| 90 | + DeprecationWarning, |
| 91 | + stacklevel=2, |
| 92 | + ) |
| 93 | + |
| 94 | + # Use the existing use() function with streaming=True |
| 95 | + model = use(client, ref, streaming=True) |
| 96 | + |
| 97 | + # Call the model with the input |
| 98 | + return model(**input) # type: ignore[return-value] |
0 commit comments