Skip to content

Commit f962940

Browse files
committed
Merge branch 'main' of github.com:Scale3-Labs/langtrace-python-sdk into ali/s3en-2623-python-sdk
2 parents 85fd414 + fa41773 commit f962940

File tree

1 file changed

+10
-6
lines changed
  • src/langtrace_python_sdk/instrumentation/ollama

1 file changed

+10
-6
lines changed

src/langtrace_python_sdk/instrumentation/ollama/patch.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,21 @@ def _handle_streaming_response(span, response, api):
168168
accumulated_tokens = None
169169
if api == "chat":
170170
accumulated_tokens = {"message": {"content": "", "role": ""}}
171-
if api == "completion":
171+
if api == "completion" or api == "generate":
172172
accumulated_tokens = {"response": ""}
173173
span.add_event(Event.STREAM_START.value)
174174
try:
175175
for chunk in response:
176+
content = None
176177
if api == "chat":
178+
content = chunk["message"]["content"]
177179
accumulated_tokens["message"]["content"] += chunk["message"]["content"]
178180
accumulated_tokens["message"]["role"] = chunk["message"]["role"]
179181
if api == "generate":
182+
content = chunk["response"]
180183
accumulated_tokens["response"] += chunk["response"]
181184

182-
set_event_completion_chunk(
183-
span, chunk.get("response") or chunk.get("message").get("content")
184-
)
185+
set_event_completion_chunk(span, content)
185186

186187
_set_response_attributes(span, chunk | accumulated_tokens)
187188
finally:
@@ -197,19 +198,22 @@ async def _ahandle_streaming_response(span, response, api):
197198
accumulated_tokens = None
198199
if api == "chat":
199200
accumulated_tokens = {"message": {"content": "", "role": ""}}
200-
if api == "completion":
201+
if api == "completion" or api == "generate":
201202
accumulated_tokens = {"response": ""}
202203

203204
span.add_event(Event.STREAM_START.value)
204205
try:
205206
async for chunk in response:
207+
content = None
206208
if api == "chat":
209+
content = chunk["message"]["content"]
207210
accumulated_tokens["message"]["content"] += chunk["message"]["content"]
208211
accumulated_tokens["message"]["role"] = chunk["message"]["role"]
209212
if api == "generate":
213+
content = chunk["response"]
210214
accumulated_tokens["response"] += chunk["response"]
211215

212-
set_event_completion_chunk(span, chunk)
216+
set_event_completion_chunk(span, content)
213217
_set_response_attributes(span, chunk | accumulated_tokens)
214218
finally:
215219
# Finalize span after processing all chunks

0 commit comments

Comments
 (0)