Skip to content

Commit 5aae390

Browse files
feat(openai)!: drop support for OpenAI SDK versions below 1.0 (#1928)
* chore!: require Python 3.11 or newer Python 3.10 reaches end of life in October 2026. Raise requires-python to >=3.11, target py311 in ruff, drop 3.10 from the CI unit matrix, and remove the pre-3.11 fallbacks for asyncio.create_task(context=...) and typing.NotRequired. Co-authored-by: Hassieb Pakzad <hassiebp@users.noreply.github.com> * feat(openai)!: drop support for OpenAI SDK versions below 1.0 The OpenAI integration now always uses the v1 client shapes. Remove the pre-1.0 method definitions and every _is_openai_v1() branch, and raise the dev dependency floor to openai>=1.0.0. Co-authored-by: Hassieb Pakzad <hassiebp@users.noreply.github.com> * fix(observe): keep detecting legacy-marked coroutine functions inspect.iscoroutinefunction ignores asyncio's _is_coroutine marker, which asgiref's markcoroutinefunction sets on Python < 3.12. Accept both so such functions keep the async wrapper and their observation ends after the coroutine runs. Co-authored-by: Hassieb Pakzad <hassiebp@users.noreply.github.com> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Hassieb Pakzad <hassiebp@users.noreply.github.com>
1 parent 41617eb commit 5aae390

3 files changed

Lines changed: 14 additions & 53 deletions

File tree

‎langfuse/openai.py‎

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,6 @@ class OpenAiDefinition:
8080
max_version: Optional[str] = None
8181

8282

83-
OPENAI_METHODS_V0 = [
84-
OpenAiDefinition(
85-
module="openai",
86-
object="ChatCompletion",
87-
method="create",
88-
type="chat",
89-
sync=True,
90-
),
91-
OpenAiDefinition(
92-
module="openai",
93-
object="Completion",
94-
method="create",
95-
type="completion",
96-
sync=True,
97-
),
98-
]
99-
100-
10183
OPENAI_METHODS_V1 = [
10284
OpenAiDefinition(
10385
module="openai.resources.chat.completions",
@@ -798,8 +780,7 @@ def _extract_streamed_openai_response(resource: Any, chunks: Any) -> Any:
798780
model, usage, finish_reason, service_tier = None, None, None, None
799781

800782
for chunk in chunks:
801-
if _is_openai_v1():
802-
chunk = chunk.__dict__
783+
chunk = chunk.__dict__
803784

804785
model = model or chunk.get("model", None) or None
805786
service_tier = service_tier or chunk.get("service_tier", None) or None
@@ -810,15 +791,14 @@ def _extract_streamed_openai_response(resource: Any, chunks: Any) -> Any:
810791
choices = chunk.get("choices") or []
811792

812793
for choice in choices:
813-
if _is_openai_v1():
814-
choice = choice.__dict__
794+
choice = choice.__dict__
815795
if resource.type == "chat":
816796
delta = choice.get("delta", None)
817797
choice_finish_reason = choice.get("finish_reason", None)
818798
if choice_finish_reason is not None:
819799
finish_reason = choice_finish_reason
820800

821-
if _is_openai_v1() and delta is not None:
801+
if delta is not None:
822802
delta = delta.__dict__
823803

824804
if delta is None:
@@ -956,7 +936,7 @@ def _get_langfuse_data_from_default_response(
956936
if len(choices) > 0:
957937
choice = choices[-1]
958938

959-
completion = choice.text if _is_openai_v1() else choice.get("text", None)
939+
completion = choice.text
960940

961941
elif resource.object == "Responses" or resource.object == "AsyncResponses":
962942
completion = _extract_response_api_completion(response.get("output", {}))
@@ -968,17 +948,11 @@ def _get_langfuse_data_from_default_response(
968948
if len(choices) > 1:
969949
completion = [
970950
_extract_chat_response(choice.message.__dict__)
971-
if _is_openai_v1()
972-
else choice.get("message", None)
973951
for choice in choices
974952
]
975953
else:
976954
choice = choices[0]
977-
completion = (
978-
_extract_chat_response(choice.message.__dict__)
979-
if _is_openai_v1()
980-
else choice.get("message", None)
981-
)
955+
completion = _extract_chat_response(choice.message.__dict__)
982956

983957
elif resource.type == "embedding":
984958
data = response.get("data") or []
@@ -1015,16 +989,12 @@ def _merge_service_tier_into_model_parameters(
1015989
return {**(model_parameters or {}), "service_tier": service_tier}
1016990

1017991

1018-
def _is_openai_v1() -> bool:
1019-
return Version(openai.__version__) >= Version("1.0.0")
1020-
1021-
1022992
def _is_streaming_response(response: Any) -> bool:
1023993
return (
1024994
isinstance(response, types.GeneratorType)
1025995
or isinstance(response, types.AsyncGeneratorType)
1026-
or (_is_openai_v1() and isinstance(response, openai.Stream))
1027-
or (_is_openai_v1() and isinstance(response, openai.AsyncStream))
996+
or isinstance(response, openai.Stream)
997+
or isinstance(response, openai.AsyncStream)
1028998
)
1029999

10301000

@@ -1034,9 +1004,6 @@ def _is_streaming_response(response: Any) -> bool:
10341004
def _install_openai_stream_iteration_hooks() -> None:
10351005
global _openai_stream_iter_hook_installed
10361006

1037-
if not _is_openai_v1():
1038-
return
1039-
10401007
if not _openai_stream_iter_hook_installed:
10411008
original_iter = openai.Stream.__iter__
10421009
original_aiter = openai.AsyncStream.__aiter__
@@ -1318,7 +1285,7 @@ def _wrap(
13181285
try:
13191286
openai_response = wrapped(**arg_extractor.get_openai_args())
13201287

1321-
if _is_openai_v1() and isinstance(openai_response, openai.Stream):
1288+
if isinstance(openai_response, openai.Stream):
13221289
return _instrument_openai_stream(
13231290
resource=open_ai_resource,
13241291
response=openai_response,
@@ -1338,9 +1305,7 @@ def _wrap(
13381305
model, completion, usage, service_tier = (
13391306
_get_langfuse_data_from_default_response(
13401307
open_ai_resource,
1341-
(parsed_response and parsed_response.__dict__)
1342-
if _is_openai_v1()
1343-
else parsed_response,
1308+
parsed_response and parsed_response.__dict__,
13441309
)
13451310
)
13461311

@@ -1407,7 +1372,7 @@ async def _wrap_async(
14071372
try:
14081373
openai_response = await wrapped(**arg_extractor.get_openai_args())
14091374

1410-
if _is_openai_v1() and isinstance(openai_response, openai.AsyncStream):
1375+
if isinstance(openai_response, openai.AsyncStream):
14111376
return _instrument_openai_async_stream(
14121377
resource=open_ai_resource,
14131378
response=openai_response,
@@ -1427,9 +1392,7 @@ async def _wrap_async(
14271392
model, completion, usage, service_tier = (
14281393
_get_langfuse_data_from_default_response(
14291394
open_ai_resource,
1430-
(parsed_response and parsed_response.__dict__)
1431-
if _is_openai_v1()
1432-
else parsed_response,
1395+
parsed_response and parsed_response.__dict__,
14331396
)
14341397
)
14351398
generation.update(
@@ -1460,9 +1423,7 @@ async def _wrap_async(
14601423

14611424

14621425
def register_tracing() -> None:
1463-
resources = OPENAI_METHODS_V1 if _is_openai_v1() else OPENAI_METHODS_V0
1464-
1465-
for resource in resources:
1426+
for resource in OPENAI_METHODS_V1:
14661427
if resource.min_version is not None and Version(openai.__version__) < Version(
14671428
resource.min_version
14681429
):

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ dev = [
5252
"pytest-httpserver>=1.0.8,<2",
5353
"ruff>=0.15.2,<0.16",
5454
"mypy>=1.0.0,<2",
55-
"openai>=0.27.8",
55+
"openai>=1.0.0",
5656
"langchain-openai>=0.0.5,<0.4",
5757
"langchain>=1,<2",
5858
"langgraph>=1,<2",

‎uv.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)