Skip to content

Commit 421d3cd

Browse files
polish: removing pydantic hard dependency and making pydantic version compatible with openai supported version.
1 parent b599b01 commit 421d3cd

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

instrumentation-genai/opentelemetry-instrumentation-openai-v2/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ dependencies = [
3030
"opentelemetry-instrumentation ~= 0.58b0",
3131
"opentelemetry-semantic-conventions ~= 0.58b0",
3232
"opentelemetry-util-genai",
33-
"pydantic >= 2, < 3",
3433
]
3534

3635
[project.optional-dependencies]

instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/response_extractors.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@
1818
from collections.abc import Mapping, Sequence
1919
from typing import TYPE_CHECKING, List, Optional, TypeVar, Union
2020

21-
from pydantic import BaseModel, ConfigDict, Field, ValidationError
21+
from pydantic import BaseModel, Field, ValidationError
22+
23+
try:
24+
from pydantic import ConfigDict
25+
26+
_PYDANTIC_V2 = True
27+
except ImportError:
28+
ConfigDict = None
29+
_PYDANTIC_V2 = False
2230

2331
from opentelemetry.semconv._incubating.attributes import (
2432
openai_attributes as OpenAIAttributes,
@@ -53,7 +61,13 @@
5361

5462

5563
class _ExtractorModel(BaseModel):
56-
model_config = ConfigDict(extra="ignore", from_attributes=True)
64+
if _PYDANTIC_V2:
65+
model_config = ConfigDict(extra="ignore", from_attributes=True)
66+
else:
67+
68+
class Config:
69+
extra = "ignore"
70+
orm_mode = True
5771

5872

5973
class _ResponseTextFormatModel(_ExtractorModel):
@@ -117,19 +131,30 @@ class _ResponsesResultModel(_ExtractorModel):
117131
usage: Optional[_UsageModel] = None
118132

119133

120-
_ResponseTextConfigModel.model_rebuild()
121-
_ResponseInputItemModel.model_rebuild()
122-
_ResponsesRequestModel.model_rebuild()
123-
_ResponseOutputItemModel.model_rebuild()
124-
_UsageModel.model_rebuild()
125-
_ResponsesResultModel.model_rebuild()
134+
def _rebuild_model(model_type: type[BaseModel]) -> None:
135+
if _PYDANTIC_V2:
136+
model_type.model_rebuild(_types_namespace=globals())
137+
else:
138+
model_type.update_forward_refs(**globals())
139+
140+
141+
_rebuild_model(_ResponseTextConfigModel)
142+
_rebuild_model(_ResponseInputItemModel)
143+
_rebuild_model(_ResponsesRequestModel)
144+
_rebuild_model(_ResponseOutputItemModel)
145+
_rebuild_model(_UsageModel)
146+
_rebuild_model(_ResponsesResultModel)
126147

127148

128149
def _validate_model(
129150
model_type: type[ModelT], value: object, context: str
130151
) -> ModelT | None:
131152
try:
132-
return model_type.model_validate(value)
153+
if _PYDANTIC_V2:
154+
return model_type.model_validate(value)
155+
if isinstance(value, Mapping):
156+
return model_type.parse_obj(value)
157+
return model_type.from_orm(value)
133158
except ValidationError:
134159
_logger.debug(
135160
"OpenAI responses extractor validation failed for %s",

uv.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)