Skip to content

Commit b0cb1c1

Browse files
committed
type checking on old openai versions
1 parent 11b9952 commit b0cb1c1

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

parea/wrapper/openai/openai.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ def convert_to_openai_object(kwargs) -> OpenAIObject:
2828
if "model" not in kwargs:
2929
kwargs["model"] = "model"
3030
if "choices" in kwargs and isinstance(kwargs["choices"], list) and len(kwargs["choices"]) > 0:
31+
if "message" in kwargs["choices"][0]:
32+
if "parsed" in kwargs["choices"][0]["message"]:
33+
kwargs["choices"][0]["message"]["content"] = json_dumps(kwargs["choices"][0]["message"]["parsed"])
34+
3135
if "finish_reason" not in kwargs["choices"][0]:
3236
kwargs["choices"][0]["finish_reason"] = "stop"
37+
3338
return OpenAIObject(**kwargs)
3439

3540

@@ -276,13 +281,15 @@ def _kwargs_to_llm_configuration(kwargs):
276281

277282
@staticmethod
278283
def _get_output(result: Any, model: Optional[str] = None) -> str:
284+
PARSED_CHAT_COMPLETION_AVAILABLE = False
279285
try:
280-
from openai.types.chat import ParsedChatCompletion, ParsedChatCompletionMessage
286+
from openai.types.chat import ParsedChatCompletionMessage
287+
288+
PARSED_CHAT_COMPLETION_AVAILABLE = True
281289
except ImportError:
282-
ParsedChatCompletion = None
283290
ParsedChatCompletionMessage = None
284291

285-
if not isinstance(result, (OpenAIObject, ParsedChatCompletion)) and isinstance(result, dict):
292+
if not isinstance(result, OpenAIObject) and isinstance(result, dict):
286293
result = convert_to_openai_object(
287294
{
288295
"choices": [
@@ -295,7 +302,7 @@ def _get_output(result: Any, model: Optional[str] = None) -> str:
295302
}
296303
)
297304
response_message = result.choices[0].message
298-
if isinstance(response_message, ParsedChatCompletionMessage):
305+
if PARSED_CHAT_COMPLETION_AVAILABLE and isinstance(response_message, ParsedChatCompletionMessage):
299306
completion = response_message.parsed.model_dump_json() if response_message.parsed else ""
300307
elif not response_message.get("content", None) if is_old_openai else not response_message.content:
301308
completion = OpenAIWrapper._format_function_call(response_message)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "poetry.core.masonry.api"
66
[tool.poetry]
77
name = "parea-ai"
88
packages = [{ include = "parea" }]
9-
version = "0.2.195"
9+
version = "0.2.196"
1010
description = "Parea python sdk"
1111
readme = "README.md"
1212
authors = ["joel-parea-ai <[email protected]>"]

0 commit comments

Comments
 (0)