Skip to content

Commit 3d1400d

Browse files
committed
feat: the decorator now returns more explicit types to support IntelliSense
1 parent 7e68f49 commit 3d1400d

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

openai_streaming/decorator.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
from collections.abc import AsyncGenerator
22
from inspect import iscoroutinefunction, signature
3-
from types import FunctionType
4-
from typing import Generator, get_origin, Union, Optional, Any, get_type_hints
3+
from typing import Generator, get_origin, Union, Optional, get_type_hints, Protocol, TypeVar, Callable
54
from typing import get_args
65

76
from docstring_parser import parse
87
from openai.types.beta import FunctionTool
8+
from openai.types.chat import ChatCompletionToolParam
99
from openai.types.shared import FunctionDefinition
1010
from pydantic import create_model
1111

1212

13-
def openai_streaming_function(func: FunctionType) -> Any:
13+
class OpenAIStreamingFunction(Protocol):
14+
"""
15+
A Protocol that represents a function that can be used with OpenAI Streaming.
16+
"""
17+
18+
openai_schema: ChatCompletionToolParam # The OpenAI Schema for the function.
19+
20+
def __call__(self, *args, **kwargs):
21+
pass
22+
23+
24+
F = TypeVar('F', bound=Callable[..., any])
25+
26+
27+
def openai_streaming_function(func: F) -> OpenAIStreamingFunction:
1428
"""
1529
Decorator that creates an OpenAI Schema for your function, while support using Generators for Streaming.
1630
@@ -75,7 +89,7 @@ async def error_message(typ: str, description: AsyncGenerator[str, None]):
7589
try:
7690
parameters = model.model_json_schema()
7791
except Exception as e:
78-
parameters = model.schema() # Fallback to the default schema
92+
parameters = model.schema() # Fallback to the default schema
7993

8094
# extract parameter documentations from the docstring
8195
for param in docstring.params:

0 commit comments

Comments
 (0)