Skip to content

Commit 7be395b

Browse files
feat: support user_id argument for OpenAI Chat completions (#354)
Co-authored-by: bryan-agicap <145330610+bryan-agicap@users.noreply.github.com>
1 parent 329ce5e commit 7be395b

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

langfuse/openai.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ def __init__(self, module: str, object: str, method: str, type: str, sync: bool)
8383

8484
class OpenAiArgsExtractor:
8585
def __init__(
86-
self, name=None, metadata=None, trace_id=None, session_id=None, **kwargs
86+
self, name=None, metadata=None, trace_id=None, session_id=None, user_id=None, **kwargs
8787
):
8888
self.args = {}
8989
self.args["name"] = name
9090
self.args["metadata"] = metadata
9191
self.args["trace_id"] = trace_id
9292
self.args["session_id"] = session_id
93+
self.args["user_id"] = user_id
9394
self.kwargs = kwargs
9495

9596
def get_langfuse_args(self):
@@ -125,11 +126,15 @@ def _get_langfuse_data_from_kwargs(
125126
if session_id is not None and not isinstance(session_id, str):
126127
raise TypeError("session_id must be a string")
127128

129+
user_id = kwargs.get("user_id", None)
130+
if user_id is not None and not isinstance(user_id, str):
131+
raise TypeError("user_id must be a string")
132+
128133
if trace_id:
129-
langfuse.trace(id=trace_id, session_id=session_id)
134+
langfuse.trace(id=trace_id, session_id=session_id, user_id=user_id)
130135
elif session_id:
131136
# If a session_id is provided but no trace_id, we should create a trace using the SDK and then use its trace_id
132-
trace_id = langfuse.trace(session_id=session_id).id
137+
trace_id = langfuse.trace(session_id=session_id, user_id=user_id).id
133138

134139
metadata = kwargs.get("metadata", {})
135140

@@ -165,6 +170,7 @@ def _get_langfuse_data_from_kwargs(
165170
"name": name,
166171
"metadata": metadata,
167172
"trace_id": trace_id,
173+
"user_id": user_id,
168174
"start_time": start_time,
169175
"input": prompt,
170176
"model_parameters": modelParameters,

tests/test_core_sdk.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ def test_create_generation():
149149

150150
timestamp = _get_timestamp()
151151
generation_id = create_uuid()
152-
153152
langfuse.generation(
154153
id=generation_id,
155154
name="query-generation",

tests/test_openai.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,28 @@ def test_openai_chat_completion_fail():
223223
openai.api_key = os.environ["OPENAI_API_KEY"]
224224

225225

226+
def test_openai_chat_completion_with_user_id():
227+
api = get_api()
228+
user_id = create_uuid()
229+
trace_id = create_uuid()
230+
completion = chat_func(
231+
name="user-creation",
232+
model="gpt-3.5-turbo",
233+
messages=[{"role": "user", "content": "1 + 1 = "}],
234+
temperature=0,
235+
metadata={"someKey": "someResponse"},
236+
user_id=user_id,
237+
trace_id=trace_id,
238+
)
239+
240+
openai.flush_langfuse()
241+
242+
assert len(completion.choices) != 0
243+
traces = api.trace.get(trace_id)
244+
245+
assert traces.user_id == user_id
246+
247+
226248
def test_openai_chat_completion_without_extra_param():
227249
completion = chat_func(
228250
model="gpt-3.5-turbo",

0 commit comments

Comments
 (0)