opentelemetry-instrumentation-openai 0.42.0__tar.gz → 0.43.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of opentelemetry-instrumentation-openai might be problematic. Click here for more details.

Files changed (21) hide show
  1. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/PKG-INFO +1 -1
  2. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/shared/__init__.py +6 -0
  3. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/shared/chat_wrappers.py +37 -28
  4. opentelemetry_instrumentation_openai-0.43.0/opentelemetry/instrumentation/openai/version.py +1 -0
  5. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/pyproject.toml +1 -1
  6. opentelemetry_instrumentation_openai-0.42.0/opentelemetry/instrumentation/openai/version.py +0 -1
  7. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/README.md +0 -0
  8. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/__init__.py +0 -0
  9. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/shared/completion_wrappers.py +0 -0
  10. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/shared/config.py +0 -0
  11. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/shared/embeddings_wrappers.py +0 -0
  12. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/shared/event_emitter.py +0 -0
  13. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/shared/event_models.py +0 -0
  14. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/shared/image_gen_wrappers.py +0 -0
  15. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/shared/span_utils.py +0 -0
  16. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/utils.py +0 -0
  17. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/v0/__init__.py +0 -0
  18. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/v1/__init__.py +0 -0
  19. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/v1/assistant_wrappers.py +0 -0
  20. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/v1/event_handler_wrapper.py +0 -0
  21. {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.0}/opentelemetry/instrumentation/openai/v1/responses_wrappers.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: opentelemetry-instrumentation-openai
3
- Version: 0.42.0
3
+ Version: 0.43.0
4
4
  Summary: OpenTelemetry OpenAI instrumentation
5
5
  License: Apache-2.0
6
6
  Author: Gal Kleinman
@@ -370,6 +370,12 @@ def get_token_count_from_string(string: str, model_name: str):
370
370
  f"Failed to get tiktoken encoding for model_name {model_name}, error: {str(ex)}"
371
371
  )
372
372
  return None
373
+ except Exception as ex:
374
+ # Other exceptions in tiktok
375
+ logger.warning(
376
+ f"Failed to get tiktoken encoding for model_name {model_name}, error: {str(ex)}"
377
+ )
378
+ return None
373
379
 
374
380
  tiktoken_encodings[model_name] = encoding
375
381
  else:
@@ -516,43 +516,45 @@ def _set_completions(span, choices):
516
516
  def _set_streaming_token_metrics(
517
517
  request_kwargs, complete_response, span, token_counter, shared_attributes
518
518
  ):
519
- # use tiktoken calculate token usage
520
519
  if not should_record_stream_token_usage():
521
520
  return
522
521
 
523
- # kwargs={'model': 'gpt-3.5', 'messages': [{'role': 'user', 'content': '...'}], 'stream': True}
524
522
  prompt_usage = -1
525
523
  completion_usage = -1
526
524
 
527
- # prompt_usage
528
- if request_kwargs and request_kwargs.get("messages"):
529
- prompt_content = ""
530
- # setting the default model_name as gpt-4. As this uses the embedding "cl100k_base" that
531
- # is used by most of the other model.
525
+ # First, try to get usage from API response
526
+ if complete_response.get("usage"):
527
+ usage = complete_response["usage"]
528
+ if usage.get("prompt_tokens"):
529
+ prompt_usage = usage["prompt_tokens"]
530
+ if usage.get("completion_tokens"):
531
+ completion_usage = usage["completion_tokens"]
532
+
533
+ # If API response doesn't have usage, fallback to tiktoken calculation
534
+ if prompt_usage == -1 or completion_usage == -1:
532
535
  model_name = (
533
536
  complete_response.get("model") or request_kwargs.get("model") or "gpt-4"
534
537
  )
535
- for msg in request_kwargs.get("messages"):
536
- if msg.get("content"):
537
- prompt_content += msg.get("content")
538
- if model_name:
539
- prompt_usage = get_token_count_from_string(prompt_content, model_name)
540
-
541
- # completion_usage
542
- if complete_response.get("choices"):
543
- completion_content = ""
544
- # setting the default model_name as gpt-4. As this uses the embedding "cl100k_base" that
545
- # is used by most of the other model.
546
- model_name = complete_response.get("model") or "gpt-4"
547
-
548
- for choice in complete_response.get("choices"):
549
- if choice.get("message") and choice.get("message").get("content"):
550
- completion_content += choice["message"]["content"]
551
-
552
- if model_name:
553
- completion_usage = get_token_count_from_string(
554
- completion_content, model_name
555
- )
538
+
539
+ # Calculate prompt tokens if not available from API
540
+ if prompt_usage == -1 and request_kwargs and request_kwargs.get("messages"):
541
+ prompt_content = ""
542
+ for msg in request_kwargs.get("messages"):
543
+ if msg.get("content"):
544
+ prompt_content += msg.get("content")
545
+ if model_name and should_record_stream_token_usage():
546
+ prompt_usage = get_token_count_from_string(prompt_content, model_name)
547
+
548
+ # Calculate completion tokens if not available from API
549
+ if completion_usage == -1 and complete_response.get("choices"):
550
+ completion_content = ""
551
+ for choice in complete_response.get("choices"):
552
+ if choice.get("message") and choice.get("message").get("content"):
553
+ completion_content += choice["message"]["content"]
554
+ if model_name and should_record_stream_token_usage():
555
+ completion_usage = get_token_count_from_string(
556
+ completion_content, model_name
557
+ )
556
558
 
557
559
  # span record
558
560
  _set_span_stream_usage(span, prompt_usage, completion_usage)
@@ -971,6 +973,13 @@ def _accumulate_stream_items(item, complete_response):
971
973
  complete_response["model"] = item.get("model")
972
974
  complete_response["id"] = item.get("id")
973
975
 
976
+ # capture usage information from the last stream chunks
977
+ if item.get("usage"):
978
+ complete_response["usage"] = item.get("usage")
979
+ elif item.get("choices") and item["choices"][0].get("usage"):
980
+ # Some LLM providers like moonshot mistakenly place token usage information within choices[0], handle this.
981
+ complete_response["usage"] = item["choices"][0].get("usage")
982
+
974
983
  # prompt filter results
975
984
  if item.get("prompt_filter_results"):
976
985
  complete_response["prompt_filter_results"] = item.get("prompt_filter_results")
@@ -8,7 +8,7 @@ show_missing = true
8
8
 
9
9
  [tool.poetry]
10
10
  name = "opentelemetry-instrumentation-openai"
11
- version = "0.42.0"
11
+ version = "0.43.0"
12
12
  description = "OpenTelemetry OpenAI instrumentation"
13
13
  authors = [
14
14
  "Gal Kleinman <gal@traceloop.com>",