judgeval 0.16.5__py3-none-any.whl → 0.16.7__py3-none-any.whl

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 judgeval might be problematic. Click here for more details.

@@ -1,6 +1,5 @@
1
1
  from __future__ import annotations
2
2
  import functools
3
- import orjson
4
3
  from typing import (
5
4
  TYPE_CHECKING,
6
5
  Any,
@@ -22,6 +21,7 @@ from judgeval.tracer.llm.llm_openai.config import (
22
21
  openai_AsyncOpenAI,
23
22
  )
24
23
  from judgeval.tracer.managers import sync_span_context, async_span_context
24
+ from judgeval.logger import judgeval_logger
25
25
  from judgeval.tracer.keys import AttributeKeys
26
26
  from judgeval.tracer.utils import set_span_attribute
27
27
  from judgeval.utils.serialize import safe_serialize
@@ -426,7 +426,7 @@ class TracedOpenAIAsyncGenerator:
426
426
  raise
427
427
 
428
428
 
429
- TClient = TypeVar("TClient", bound=OpenAIClient)
429
+ TClient = TypeVar("TClient", bound=Union[OpenAIClient, OpenAIAsyncClient])
430
430
 
431
431
 
432
432
  def wrap_openai_client(tracer: Tracer, client: TClient) -> TClient:
@@ -457,66 +457,78 @@ def wrap_openai_client(tracer: Tracer, client: TClient) -> TClient:
457
457
  with sync_span_context(
458
458
  tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
459
459
  ) as span:
460
- tracer.add_agent_attributes_to_span(span)
461
- set_span_attribute(
462
- span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
463
- )
464
- model_name = kwargs.get("model", "")
465
- set_span_attribute(
466
- span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name
467
- )
468
- response = function(*args, **kwargs)
469
-
470
- if isinstance(response, (OpenAIChatCompletionBase, OpenAIResponse)):
471
- output, usage_data = _format_openai_output(response)
472
- # Serialize structured data to JSON for span attribute
473
- if isinstance(output, list):
474
- output_str = orjson.dumps(
475
- output, option=orjson.OPT_INDENT_2
476
- ).decode()
477
- else:
478
- output_str = str(output) if output is not None else None
460
+ try:
461
+ tracer.add_agent_attributes_to_span(span)
479
462
  set_span_attribute(
480
- span, AttributeKeys.GEN_AI_COMPLETION, output_str
463
+ span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
481
464
  )
482
- if usage_data:
483
- (
484
- prompt_tokens,
485
- completion_tokens,
486
- cache_read,
487
- cache_creation,
488
- ) = _extract_openai_tokens(usage_data)
489
- set_span_attribute(
490
- span,
491
- AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
492
- prompt_tokens,
493
- )
494
- set_span_attribute(
495
- span,
496
- AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
497
- completion_tokens,
498
- )
499
- set_span_attribute(
500
- span,
501
- AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
502
- cache_read,
503
- )
465
+ model_name = kwargs.get("model", "")
466
+ set_span_attribute(
467
+ span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name
468
+ )
469
+ except Exception as e:
470
+ judgeval_logger.error(
471
+ f"[openai wrapped] Error adding span metadata: {e}"
472
+ )
473
+
474
+ response = function(*args, **kwargs)
475
+
476
+ try:
477
+ if isinstance(
478
+ response, (OpenAIChatCompletionBase, OpenAIResponse)
479
+ ):
480
+ output, usage_data = _format_openai_output(response)
481
+ # Serialize structured data to JSON for span attribute
482
+ if isinstance(output, list):
483
+ output_str = safe_serialize(output)
484
+ else:
485
+ output_str = str(output) if output is not None else None
504
486
  set_span_attribute(
505
- span,
506
- AttributeKeys.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
507
- cache_creation,
487
+ span, AttributeKeys.GEN_AI_COMPLETION, output_str
508
488
  )
489
+ if usage_data:
490
+ (
491
+ prompt_tokens,
492
+ completion_tokens,
493
+ cache_read,
494
+ cache_creation,
495
+ ) = _extract_openai_tokens(usage_data)
496
+ set_span_attribute(
497
+ span,
498
+ AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
499
+ prompt_tokens,
500
+ )
501
+ set_span_attribute(
502
+ span,
503
+ AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
504
+ completion_tokens,
505
+ )
506
+ set_span_attribute(
507
+ span,
508
+ AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
509
+ cache_read,
510
+ )
511
+ set_span_attribute(
512
+ span,
513
+ AttributeKeys.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
514
+ cache_creation,
515
+ )
516
+ set_span_attribute(
517
+ span,
518
+ AttributeKeys.JUDGMENT_USAGE_METADATA,
519
+ safe_serialize(usage_data),
520
+ )
509
521
  set_span_attribute(
510
522
  span,
511
- AttributeKeys.JUDGMENT_USAGE_METADATA,
512
- safe_serialize(usage_data),
523
+ AttributeKeys.GEN_AI_RESPONSE_MODEL,
524
+ getattr(response, "model", model_name),
513
525
  )
514
- set_span_attribute(
515
- span,
516
- AttributeKeys.GEN_AI_RESPONSE_MODEL,
517
- getattr(response, "model", model_name),
526
+ except Exception as e:
527
+ judgeval_logger.error(
528
+ f"[openai wrapped] Error adding span metadata: {e}"
518
529
  )
519
- return response
530
+ finally:
531
+ return response
520
532
 
521
533
  return wrapper
522
534
 
@@ -541,66 +553,78 @@ def wrap_openai_client(tracer: Tracer, client: TClient) -> TClient:
541
553
  async with async_span_context(
542
554
  tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
543
555
  ) as span:
544
- tracer.add_agent_attributes_to_span(span)
545
- set_span_attribute(
546
- span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
547
- )
548
- model_name = kwargs.get("model", "")
549
- set_span_attribute(
550
- span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name
551
- )
552
- response = await function(*args, **kwargs)
553
-
554
- if isinstance(response, (OpenAIChatCompletionBase, OpenAIResponse)):
555
- output, usage_data = _format_openai_output(response)
556
- # Serialize structured data to JSON for span attribute
557
- if isinstance(output, list):
558
- output_str = orjson.dumps(
559
- output, option=orjson.OPT_INDENT_2
560
- ).decode()
561
- else:
562
- output_str = str(output) if output is not None else None
556
+ try:
557
+ tracer.add_agent_attributes_to_span(span)
563
558
  set_span_attribute(
564
- span, AttributeKeys.GEN_AI_COMPLETION, output_str
559
+ span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
565
560
  )
566
- if usage_data:
567
- (
568
- prompt_tokens,
569
- completion_tokens,
570
- cache_read,
571
- cache_creation,
572
- ) = _extract_openai_tokens(usage_data)
573
- set_span_attribute(
574
- span,
575
- AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
576
- prompt_tokens,
577
- )
578
- set_span_attribute(
579
- span,
580
- AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
581
- completion_tokens,
582
- )
583
- set_span_attribute(
584
- span,
585
- AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
586
- cache_read,
587
- )
561
+ model_name = kwargs.get("model", "")
562
+ set_span_attribute(
563
+ span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name
564
+ )
565
+ except Exception as e:
566
+ judgeval_logger.error(
567
+ f"[openai wrapped_async] Error adding span metadata: {e}"
568
+ )
569
+
570
+ response = await function(*args, **kwargs)
571
+
572
+ try:
573
+ if isinstance(
574
+ response, (OpenAIChatCompletionBase, OpenAIResponse)
575
+ ):
576
+ output, usage_data = _format_openai_output(response)
577
+ # Serialize structured data to JSON for span attribute
578
+ if isinstance(output, list):
579
+ output_str = safe_serialize(output)
580
+ else:
581
+ output_str = str(output) if output is not None else None
588
582
  set_span_attribute(
589
- span,
590
- AttributeKeys.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
591
- cache_creation,
583
+ span, AttributeKeys.GEN_AI_COMPLETION, output_str
592
584
  )
585
+ if usage_data:
586
+ (
587
+ prompt_tokens,
588
+ completion_tokens,
589
+ cache_read,
590
+ cache_creation,
591
+ ) = _extract_openai_tokens(usage_data)
592
+ set_span_attribute(
593
+ span,
594
+ AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
595
+ prompt_tokens,
596
+ )
597
+ set_span_attribute(
598
+ span,
599
+ AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
600
+ completion_tokens,
601
+ )
602
+ set_span_attribute(
603
+ span,
604
+ AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
605
+ cache_read,
606
+ )
607
+ set_span_attribute(
608
+ span,
609
+ AttributeKeys.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
610
+ cache_creation,
611
+ )
612
+ set_span_attribute(
613
+ span,
614
+ AttributeKeys.JUDGMENT_USAGE_METADATA,
615
+ safe_serialize(usage_data),
616
+ )
593
617
  set_span_attribute(
594
618
  span,
595
- AttributeKeys.JUDGMENT_USAGE_METADATA,
596
- safe_serialize(usage_data),
619
+ AttributeKeys.GEN_AI_RESPONSE_MODEL,
620
+ getattr(response, "model", model_name),
597
621
  )
598
- set_span_attribute(
599
- span,
600
- AttributeKeys.GEN_AI_RESPONSE_MODEL,
601
- getattr(response, "model", model_name),
622
+ except Exception as e:
623
+ judgeval_logger.error(
624
+ f"[openai wrapped_async] Error adding span metadata: {e}"
602
625
  )
603
- return response
626
+ finally:
627
+ return response
604
628
 
605
629
  return wrapper
606
630
 
@@ -19,6 +19,7 @@ from judgeval.tracer.llm.llm_together.config import (
19
19
  together_AsyncTogether,
20
20
  )
21
21
  from judgeval.tracer.managers import sync_span_context, async_span_context
22
+ from judgeval.logger import judgeval_logger
22
23
  from judgeval.tracer.keys import AttributeKeys
23
24
  from judgeval.tracer.utils import set_span_attribute
24
25
  from judgeval.utils.serialize import safe_serialize
@@ -296,73 +297,85 @@ def wrap_together_client(
296
297
  with sync_span_context(
297
298
  tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
298
299
  ) as span:
299
- tracer.add_agent_attributes_to_span(span)
300
- set_span_attribute(
301
- span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
302
- )
303
- model_name = kwargs.get("model", "")
304
- # Add together_ai/ prefix for server-side cost calculation
305
- prefixed_model_name = (
306
- f"together_ai/{model_name}" if model_name else ""
307
- )
308
- set_span_attribute(
309
- span, AttributeKeys.GEN_AI_REQUEST_MODEL, prefixed_model_name
310
- )
300
+ try:
301
+ tracer.add_agent_attributes_to_span(span)
302
+ set_span_attribute(
303
+ span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
304
+ )
305
+ model_name = kwargs.get("model", "")
306
+ # Add together_ai/ prefix for server-side cost calculation
307
+ prefixed_model_name = (
308
+ f"together_ai/{model_name}" if model_name else ""
309
+ )
310
+ set_span_attribute(
311
+ span,
312
+ AttributeKeys.GEN_AI_REQUEST_MODEL,
313
+ prefixed_model_name,
314
+ )
315
+ except Exception as e:
316
+ judgeval_logger.error(
317
+ f"[together wrapped] Error adding span metadata: {e}"
318
+ )
319
+
311
320
  response = function(*args, **kwargs)
312
321
 
313
- if isinstance(response, TogetherChatCompletion):
314
- output, usage_data = _format_together_output(response)
315
- # Serialize structured data to JSON for span attribute
316
- if output:
317
- if isinstance(output, list):
318
- import orjson
319
-
320
- output_str = orjson.dumps(
321
- output, option=orjson.OPT_INDENT_2
322
- ).decode()
323
- else:
324
- output_str = str(output)
325
- set_span_attribute(
326
- span, AttributeKeys.GEN_AI_COMPLETION, output_str
322
+ try:
323
+ if isinstance(response, TogetherChatCompletion):
324
+ output, usage_data = _format_together_output(response)
325
+ # Serialize structured data to JSON for span attribute
326
+ if output:
327
+ if isinstance(output, list):
328
+ output_str = safe_serialize(output)
329
+ else:
330
+ output_str = str(output)
331
+ set_span_attribute(
332
+ span, AttributeKeys.GEN_AI_COMPLETION, output_str
333
+ )
334
+ if usage_data:
335
+ (
336
+ prompt_tokens,
337
+ completion_tokens,
338
+ cache_read,
339
+ cache_creation,
340
+ ) = _extract_together_tokens(usage_data)
341
+ set_span_attribute(
342
+ span,
343
+ AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
344
+ prompt_tokens,
345
+ )
346
+ set_span_attribute(
347
+ span,
348
+ AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
349
+ completion_tokens,
350
+ )
351
+ set_span_attribute(
352
+ span,
353
+ AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
354
+ cache_read,
355
+ )
356
+ set_span_attribute(
357
+ span,
358
+ AttributeKeys.JUDGMENT_USAGE_METADATA,
359
+ safe_serialize(usage_data),
360
+ )
361
+ # Add together_ai/ prefix to response model for server-side cost calculation
362
+ response_model = getattr(response, "model", model_name)
363
+ prefixed_response_model = (
364
+ f"together_ai/{response_model}"
365
+ if response_model
366
+ else ""
327
367
  )
328
- if usage_data:
329
- (
330
- prompt_tokens,
331
- completion_tokens,
332
- cache_read,
333
- cache_creation,
334
- ) = _extract_together_tokens(usage_data)
335
368
  set_span_attribute(
336
369
  span,
337
- AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
338
- prompt_tokens,
370
+ AttributeKeys.GEN_AI_RESPONSE_MODEL,
371
+ prefixed_response_model,
339
372
  )
340
- set_span_attribute(
341
- span,
342
- AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
343
- completion_tokens,
344
- )
345
- set_span_attribute(
346
- span,
347
- AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
348
- cache_read,
349
- )
350
- set_span_attribute(
351
- span,
352
- AttributeKeys.JUDGMENT_USAGE_METADATA,
353
- safe_serialize(usage_data),
354
- )
355
- # Add together_ai/ prefix to response model for server-side cost calculation
356
- response_model = getattr(response, "model", model_name)
357
- prefixed_response_model = (
358
- f"together_ai/{response_model}" if response_model else ""
359
- )
360
- set_span_attribute(
361
- span,
362
- AttributeKeys.GEN_AI_RESPONSE_MODEL,
363
- prefixed_response_model,
373
+ except Exception as e:
374
+ judgeval_logger.error(
375
+ f"[together wrapped] Error adding span metadata: {e}"
364
376
  )
365
- return response
377
+ finally:
378
+ return response
366
379
 
367
380
  return wrapper
368
381
 
@@ -391,73 +404,85 @@ def wrap_together_client(
391
404
  async with async_span_context(
392
405
  tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
393
406
  ) as span:
394
- tracer.add_agent_attributes_to_span(span)
395
- set_span_attribute(
396
- span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
397
- )
398
- model_name = kwargs.get("model", "")
399
- # Add together_ai/ prefix for server-side cost calculation
400
- prefixed_model_name = (
401
- f"together_ai/{model_name}" if model_name else ""
402
- )
403
- set_span_attribute(
404
- span, AttributeKeys.GEN_AI_REQUEST_MODEL, prefixed_model_name
405
- )
407
+ try:
408
+ tracer.add_agent_attributes_to_span(span)
409
+ set_span_attribute(
410
+ span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
411
+ )
412
+ model_name = kwargs.get("model", "")
413
+ # Add together_ai/ prefix for server-side cost calculation
414
+ prefixed_model_name = (
415
+ f"together_ai/{model_name}" if model_name else ""
416
+ )
417
+ set_span_attribute(
418
+ span,
419
+ AttributeKeys.GEN_AI_REQUEST_MODEL,
420
+ prefixed_model_name,
421
+ )
422
+ except Exception as e:
423
+ judgeval_logger.error(
424
+ f"[together wrapped_async] Error adding span metadata: {e}"
425
+ )
426
+
406
427
  response = await function(*args, **kwargs)
407
428
 
408
- if isinstance(response, TogetherChatCompletion):
409
- output, usage_data = _format_together_output(response)
410
- # Serialize structured data to JSON for span attribute
411
- if output:
412
- if isinstance(output, list):
413
- import orjson
414
-
415
- output_str = orjson.dumps(
416
- output, option=orjson.OPT_INDENT_2
417
- ).decode()
418
- else:
419
- output_str = str(output)
420
- set_span_attribute(
421
- span, AttributeKeys.GEN_AI_COMPLETION, output_str
429
+ try:
430
+ if isinstance(response, TogetherChatCompletion):
431
+ output, usage_data = _format_together_output(response)
432
+ # Serialize structured data to JSON for span attribute
433
+ if output:
434
+ if isinstance(output, list):
435
+ output_str = safe_serialize(output)
436
+ else:
437
+ output_str = str(output)
438
+ set_span_attribute(
439
+ span, AttributeKeys.GEN_AI_COMPLETION, output_str
440
+ )
441
+ if usage_data:
442
+ (
443
+ prompt_tokens,
444
+ completion_tokens,
445
+ cache_read,
446
+ cache_creation,
447
+ ) = _extract_together_tokens(usage_data)
448
+ set_span_attribute(
449
+ span,
450
+ AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
451
+ prompt_tokens,
452
+ )
453
+ set_span_attribute(
454
+ span,
455
+ AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
456
+ completion_tokens,
457
+ )
458
+ set_span_attribute(
459
+ span,
460
+ AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
461
+ cache_read,
462
+ )
463
+ set_span_attribute(
464
+ span,
465
+ AttributeKeys.JUDGMENT_USAGE_METADATA,
466
+ safe_serialize(usage_data),
467
+ )
468
+ # Add together_ai/ prefix to response model for server-side cost calculation
469
+ response_model = getattr(response, "model", model_name)
470
+ prefixed_response_model = (
471
+ f"together_ai/{response_model}"
472
+ if response_model
473
+ else ""
422
474
  )
423
- if usage_data:
424
- (
425
- prompt_tokens,
426
- completion_tokens,
427
- cache_read,
428
- cache_creation,
429
- ) = _extract_together_tokens(usage_data)
430
475
  set_span_attribute(
431
476
  span,
432
- AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
433
- prompt_tokens,
477
+ AttributeKeys.GEN_AI_RESPONSE_MODEL,
478
+ prefixed_response_model,
434
479
  )
435
- set_span_attribute(
436
- span,
437
- AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
438
- completion_tokens,
439
- )
440
- set_span_attribute(
441
- span,
442
- AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
443
- cache_read,
444
- )
445
- set_span_attribute(
446
- span,
447
- AttributeKeys.JUDGMENT_USAGE_METADATA,
448
- safe_serialize(usage_data),
449
- )
450
- # Add together_ai/ prefix to response model for server-side cost calculation
451
- response_model = getattr(response, "model", model_name)
452
- prefixed_response_model = (
453
- f"together_ai/{response_model}" if response_model else ""
454
- )
455
- set_span_attribute(
456
- span,
457
- AttributeKeys.GEN_AI_RESPONSE_MODEL,
458
- prefixed_response_model,
480
+ except Exception as e:
481
+ judgeval_logger.error(
482
+ f"[together wrapped_async] Error adding span metadata: {e}"
459
483
  )
460
- return response
484
+ finally:
485
+ return response
461
486
 
462
487
  return wrapper
463
488
 
judgeval/tracer/utils.py CHANGED
@@ -13,7 +13,7 @@ def set_span_attribute(span: Span, name: str, value: Any):
13
13
 
14
14
 
15
15
  class TraceScorerConfig(BaseModel):
16
- scorer: TraceAPIScorerConfig
16
+ scorer: TraceAPIScorerConfig | None
17
17
  model: Optional[str] = None
18
18
  sampling_rate: float = 1.0
19
19
  run_condition: Optional[Callable[..., bool]] = None
@@ -13,7 +13,7 @@ def dont_throw(func: Callable[..., T]) -> Callable[..., T | None]:
13
13
  try:
14
14
  return func(*args, **kwargs)
15
15
  except Exception as e:
16
- judgeval_logger.debug(
16
+ judgeval_logger.warning(
17
17
  f"An exception was raised in {func.__name__}", exc_info=e
18
18
  )
19
19
  pass
judgeval/version.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = "0.16.5"
1
+ __version__ = "0.16.7"
2
2
 
3
3
 
4
4
  def get_version() -> str:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: judgeval
3
- Version: 0.16.5
3
+ Version: 0.16.7
4
4
  Summary: Judgeval Package
5
5
  Project-URL: Homepage, https://github.com/JudgmentLabs/judgeval
6
6
  Project-URL: Issues, https://github.com/JudgmentLabs/judgeval/issues