Skip to content

Commit 0ffda1c

Browse files
committed
fix: #4 supports older pydantic as well
1 parent 07b6ee2 commit 0ffda1c

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

openai_streaming/decorator.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import get_args
66

77
from docstring_parser import parse
8-
from openai.types.beta.assistant import ToolFunction
8+
from openai.types.beta import FunctionTool
99
from openai.types.shared import FunctionDefinition
1010
from pydantic import create_model
1111

@@ -72,14 +72,17 @@ async def error_message(typ: str, description: AsyncGenerator[str, None]):
7272
docstring = parse(func.__doc__ or "")
7373

7474
# prepare the parameters(arguments)
75-
parameters = model.model_json_schema()
75+
try:
76+
parameters = model.model_json_schema()
77+
except Exception as e:
78+
parameters = model.schema() # Fallback to the default schema
7679

7780
# extract parameter documentations from the docstring
7881
for param in docstring.params:
7982
if (name := param.arg_name) in parameters["properties"] and (description := param.description):
8083
parameters["properties"][name]["description"] = description
8184

82-
func.openai_schema = ToolFunction(type='function', function=FunctionDefinition(
85+
func.openai_schema = FunctionTool(type='function', function=FunctionDefinition(
8386
name=func.__name__,
8487
description=docstring.short_description,
8588
parameters=parameters,

openai_streaming/stream_processing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def _process_message(
222222
:return: Generator
223223
"""
224224
choice = message.choices[0]
225-
if not choice.model_fields.get("delta"):
225+
if not hasattr(choice, "delta"):
226226
raise LookupError("No delta in choice")
227227

228228
delta = message.choices[0].delta

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ license = {text = "MIT"}
1111
readme = "README.md"
1212
keywords = ["openai", "gpt", "llm", "streaming", "stream", "generator"]
1313
dependencies= [
14-
"openai>=1.0.0,<2.0.0",
14+
"openai>=1.14.0,<2.0.0",
1515
"json-streamer>=0.1.0,<0.2.0",
1616
"pydantic>=2.0.2,<3.0.0",
1717
"docstring-parser>=0.15"

requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
openai==1.11.1
1+
openai==1.14.0
22
json-streamer==0.1.0
3-
pydantic==2.6.1
3+
pydantic==2.6.4
44
docstring-parser==0.15

0 commit comments

Comments
 (0)