@@ -99,7 +99,9 @@ def _instrument(self, **kwargs):
99
99
100
100
register_post_import_hook (self ._patch , "openai" )
101
101
102
- def _patch (self , _module ):
102
+ def _patch (self , module ):
103
+ version = tuple ([int (x ) for x in getattr (getattr (module , "version" ), "VERSION" ).split ("." )])
104
+ self .beta_chat_available = version >= (1 , 40 , 0 )
103
105
wrap_function_wrapper (
104
106
"openai.resources.chat.completions" ,
105
107
"Completions.create" ,
@@ -110,6 +112,17 @@ def _patch(self, _module):
110
112
"AsyncCompletions.create" ,
111
113
self ._async_chat_completion_wrapper ,
112
114
)
115
+ if self .beta_chat_available :
116
+ wrap_function_wrapper (
117
+ "openai.resources.beta.chat.completions" ,
118
+ "Completions.parse" ,
119
+ self ._chat_completion_wrapper ,
120
+ )
121
+ wrap_function_wrapper (
122
+ "openai.resources.beta.chat.completions" ,
123
+ "AsyncCompletions.parse" ,
124
+ self ._async_chat_completion_wrapper ,
125
+ )
113
126
wrap_function_wrapper (
114
127
"openai.resources.embeddings" ,
115
128
"Embeddings.create" ,
@@ -128,11 +141,14 @@ def _uninstrument(self, **kwargs):
128
141
129
142
unwrap (openai .resources .chat .completions .Completions , "create" )
130
143
unwrap (openai .resources .chat .completions .AsyncCompletions , "create" )
144
+ if self .beta_chat_available :
145
+ unwrap (openai .resources .beta .chat .completions .Completions , "parse" )
146
+ unwrap (openai .resources .beta .chat .completions .AsyncCompletions , "parse" )
131
147
unwrap (openai .resources .embeddings .Embeddings , "create" )
132
148
unwrap (openai .resources .embeddings .AsyncEmbeddings , "create" )
133
149
134
150
def _chat_completion_wrapper (self , wrapped , instance , args , kwargs ):
135
- logger .debug (f"openai.resources.chat.completions.Completions.create kwargs: { kwargs } " )
151
+ logger .debug (f"{ wrapped } kwargs: { kwargs } " )
136
152
137
153
span_attributes = _get_attributes_from_wrapper (instance , kwargs )
138
154
event_attributes = _get_event_attributes ()
0 commit comments