judgeval 0.16.5__py3-none-any.whl → 0.16.6__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.

@@ -18,6 +18,7 @@ from judgeval.tracer.llm.llm_google.config import (
18
18
  google_genai_AsyncClient,
19
19
  )
20
20
  from judgeval.tracer.managers import sync_span_context, async_span_context
21
+ from judgeval.logger import judgeval_logger
21
22
  from judgeval.tracer.keys import AttributeKeys
22
23
  from judgeval.tracer.utils import set_span_attribute
23
24
  from judgeval.utils.serialize import safe_serialize
@@ -263,23 +264,10 @@ def wrap_google_client(tracer: Tracer, client: GoogleClientType) -> GoogleClient
263
264
  @functools.wraps(function)
264
265
  def wrapper(*args, **kwargs):
265
266
  if kwargs.get("stream", False):
266
- span = tracer.get_tracer().start_span(
267
- span_name, attributes={AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
268
- )
269
- tracer.add_agent_attributes_to_span(span)
270
- set_span_attribute(
271
- span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
272
- )
273
- model_name = kwargs.get("model", "")
274
- set_span_attribute(span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name)
275
- stream_response = function(*args, **kwargs)
276
- return TracedGoogleGenerator(
277
- tracer, stream_response, client, span, model_name
278
- )
279
- else:
280
- with sync_span_context(
281
- tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
282
- ) as span:
267
+ try:
268
+ span = tracer.get_tracer().start_span(
269
+ span_name, attributes={AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
270
+ )
283
271
  tracer.add_agent_attributes_to_span(span)
284
272
  set_span_attribute(
285
273
  span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
@@ -288,46 +276,78 @@ def wrap_google_client(tracer: Tracer, client: GoogleClientType) -> GoogleClient
288
276
  set_span_attribute(
289
277
  span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name
290
278
  )
291
- response = function(*args, **kwargs)
292
-
293
- if isinstance(response, GoogleGenerateContentResponse):
294
- output, usage_data = _format_google_output(response)
279
+ except Exception as e:
280
+ judgeval_logger.error(
281
+ f"[google wrapped] Error adding span metadata: {e}"
282
+ )
283
+ stream_response = function(*args, **kwargs)
284
+ return TracedGoogleGenerator(
285
+ tracer, stream_response, client, span, model_name
286
+ )
287
+ else:
288
+ with sync_span_context(
289
+ tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
290
+ ) as span:
291
+ try:
292
+ tracer.add_agent_attributes_to_span(span)
295
293
  set_span_attribute(
296
- span, AttributeKeys.GEN_AI_COMPLETION, output
294
+ span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
297
295
  )
298
- if usage_data:
299
- (
300
- prompt_tokens,
301
- completion_tokens,
302
- cache_read,
303
- cache_creation,
304
- ) = _extract_google_tokens(usage_data)
305
- set_span_attribute(
306
- span,
307
- AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
308
- prompt_tokens,
309
- )
310
- set_span_attribute(
311
- span,
312
- AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
313
- completion_tokens,
314
- )
296
+ model_name = kwargs.get("model", "")
297
+ set_span_attribute(
298
+ span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name
299
+ )
300
+ except Exception as e:
301
+ judgeval_logger.error(
302
+ f"[google wrapped] Error adding span metadata: {e}"
303
+ )
304
+
305
+ response = function(*args, **kwargs)
306
+
307
+ try:
308
+ if isinstance(response, GoogleGenerateContentResponse):
309
+ output, usage_data = _format_google_output(response)
315
310
  set_span_attribute(
316
- span,
317
- AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
318
- cache_read,
311
+ span, AttributeKeys.GEN_AI_COMPLETION, output
319
312
  )
313
+ if usage_data:
314
+ (
315
+ prompt_tokens,
316
+ completion_tokens,
317
+ cache_read,
318
+ cache_creation,
319
+ ) = _extract_google_tokens(usage_data)
320
+ set_span_attribute(
321
+ span,
322
+ AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
323
+ prompt_tokens,
324
+ )
325
+ set_span_attribute(
326
+ span,
327
+ AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
328
+ completion_tokens,
329
+ )
330
+ set_span_attribute(
331
+ span,
332
+ AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
333
+ cache_read,
334
+ )
335
+ set_span_attribute(
336
+ span,
337
+ AttributeKeys.JUDGMENT_USAGE_METADATA,
338
+ safe_serialize(usage_data),
339
+ )
320
340
  set_span_attribute(
321
341
  span,
322
- AttributeKeys.JUDGMENT_USAGE_METADATA,
323
- safe_serialize(usage_data),
342
+ AttributeKeys.GEN_AI_RESPONSE_MODEL,
343
+ getattr(response, "model_version", model_name),
324
344
  )
325
- set_span_attribute(
326
- span,
327
- AttributeKeys.GEN_AI_RESPONSE_MODEL,
328
- getattr(response, "model_version", model_name),
345
+ except Exception as e:
346
+ judgeval_logger.error(
347
+ f"[google wrapped] Error adding span metadata: {e}"
329
348
  )
330
- return response
349
+ finally:
350
+ return response
331
351
 
332
352
  return wrapper
333
353
 
@@ -335,23 +355,10 @@ def wrap_google_client(tracer: Tracer, client: GoogleClientType) -> GoogleClient
335
355
  @functools.wraps(function)
336
356
  async def wrapper(*args, **kwargs):
337
357
  if kwargs.get("stream", False):
338
- span = tracer.get_tracer().start_span(
339
- span_name, attributes={AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
340
- )
341
- tracer.add_agent_attributes_to_span(span)
342
- set_span_attribute(
343
- span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
344
- )
345
- model_name = kwargs.get("model", "")
346
- set_span_attribute(span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name)
347
- stream_response = await function(*args, **kwargs)
348
- return TracedGoogleAsyncGenerator(
349
- tracer, stream_response, client, span, model_name
350
- )
351
- else:
352
- async with async_span_context(
353
- tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
354
- ) as span:
358
+ try:
359
+ span = tracer.get_tracer().start_span(
360
+ span_name, attributes={AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
361
+ )
355
362
  tracer.add_agent_attributes_to_span(span)
356
363
  set_span_attribute(
357
364
  span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
@@ -360,46 +367,78 @@ def wrap_google_client(tracer: Tracer, client: GoogleClientType) -> GoogleClient
360
367
  set_span_attribute(
361
368
  span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name
362
369
  )
363
- response = await function(*args, **kwargs)
364
-
365
- if isinstance(response, GoogleGenerateContentResponse):
366
- output, usage_data = _format_google_output(response)
370
+ except Exception as e:
371
+ judgeval_logger.error(
372
+ f"[google wrapped_async] Error adding span metadata: {e}"
373
+ )
374
+ stream_response = await function(*args, **kwargs)
375
+ return TracedGoogleAsyncGenerator(
376
+ tracer, stream_response, client, span, model_name
377
+ )
378
+ else:
379
+ async with async_span_context(
380
+ tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
381
+ ) as span:
382
+ try:
383
+ tracer.add_agent_attributes_to_span(span)
367
384
  set_span_attribute(
368
- span, AttributeKeys.GEN_AI_COMPLETION, output
385
+ span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
369
386
  )
370
- if usage_data:
371
- (
372
- prompt_tokens,
373
- completion_tokens,
374
- cache_read,
375
- cache_creation,
376
- ) = _extract_google_tokens(usage_data)
377
- set_span_attribute(
378
- span,
379
- AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
380
- prompt_tokens,
381
- )
382
- set_span_attribute(
383
- span,
384
- AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
385
- completion_tokens,
386
- )
387
+ model_name = kwargs.get("model", "")
388
+ set_span_attribute(
389
+ span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name
390
+ )
391
+ except Exception as e:
392
+ judgeval_logger.error(
393
+ f"[google wrapped_async] Error adding span metadata: {e}"
394
+ )
395
+
396
+ response = await function(*args, **kwargs)
397
+
398
+ try:
399
+ if isinstance(response, GoogleGenerateContentResponse):
400
+ output, usage_data = _format_google_output(response)
387
401
  set_span_attribute(
388
- span,
389
- AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
390
- cache_read,
402
+ span, AttributeKeys.GEN_AI_COMPLETION, output
391
403
  )
404
+ if usage_data:
405
+ (
406
+ prompt_tokens,
407
+ completion_tokens,
408
+ cache_read,
409
+ cache_creation,
410
+ ) = _extract_google_tokens(usage_data)
411
+ set_span_attribute(
412
+ span,
413
+ AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
414
+ prompt_tokens,
415
+ )
416
+ set_span_attribute(
417
+ span,
418
+ AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
419
+ completion_tokens,
420
+ )
421
+ set_span_attribute(
422
+ span,
423
+ AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
424
+ cache_read,
425
+ )
426
+ set_span_attribute(
427
+ span,
428
+ AttributeKeys.JUDGMENT_USAGE_METADATA,
429
+ safe_serialize(usage_data),
430
+ )
392
431
  set_span_attribute(
393
432
  span,
394
- AttributeKeys.JUDGMENT_USAGE_METADATA,
395
- safe_serialize(usage_data),
433
+ AttributeKeys.GEN_AI_RESPONSE_MODEL,
434
+ getattr(response, "model_version", model_name),
396
435
  )
397
- set_span_attribute(
398
- span,
399
- AttributeKeys.GEN_AI_RESPONSE_MODEL,
400
- getattr(response, "model_version", model_name),
436
+ except Exception as e:
437
+ judgeval_logger.error(
438
+ f"[google wrapped_async] Error adding span metadata: {e}"
401
439
  )
402
- return response
440
+ finally:
441
+ return response
403
442
 
404
443
  return wrapper
405
444
 
@@ -19,6 +19,7 @@ from judgeval.tracer.llm.llm_groq.config import (
19
19
  groq_AsyncGroq,
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
@@ -295,71 +296,81 @@ def wrap_groq_client(tracer: Tracer, client: GroqClientType) -> GroqClientType:
295
296
  with sync_span_context(
296
297
  tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
297
298
  ) as span:
298
- tracer.add_agent_attributes_to_span(span)
299
- set_span_attribute(
300
- span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
301
- )
302
- model_name = kwargs.get("model", "")
303
- # Add groq/ prefix for server-side cost calculation
304
- prefixed_model_name = f"groq/{model_name}" if model_name else ""
305
- set_span_attribute(
306
- span, AttributeKeys.GEN_AI_REQUEST_MODEL, prefixed_model_name
307
- )
299
+ try:
300
+ tracer.add_agent_attributes_to_span(span)
301
+ set_span_attribute(
302
+ span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
303
+ )
304
+ model_name = kwargs.get("model", "")
305
+ # Add groq/ prefix for server-side cost calculation
306
+ prefixed_model_name = f"groq/{model_name}" if model_name else ""
307
+ set_span_attribute(
308
+ span,
309
+ AttributeKeys.GEN_AI_REQUEST_MODEL,
310
+ prefixed_model_name,
311
+ )
312
+ except Exception as e:
313
+ judgeval_logger.error(
314
+ f"[groq wrapped] Error adding span metadata: {e}"
315
+ )
316
+
308
317
  response = function(*args, **kwargs)
309
318
 
310
- if isinstance(response, GroqChatCompletion):
311
- output, usage_data = _format_groq_output(response)
312
- # Serialize structured data to JSON for span attribute
313
- if output:
314
- if isinstance(output, list):
315
- import orjson
316
-
317
- output_str = orjson.dumps(
318
- output, option=orjson.OPT_INDENT_2
319
- ).decode()
320
- else:
321
- output_str = str(output)
322
- set_span_attribute(
323
- span, AttributeKeys.GEN_AI_COMPLETION, output_str
324
- )
325
- if usage_data:
326
- (
327
- prompt_tokens,
328
- completion_tokens,
329
- cache_read,
330
- cache_creation,
331
- ) = _extract_groq_tokens(usage_data)
332
- set_span_attribute(
333
- span,
334
- AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
335
- prompt_tokens,
336
- )
337
- set_span_attribute(
338
- span,
339
- AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
340
- completion_tokens,
341
- )
342
- set_span_attribute(
343
- span,
344
- AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
345
- cache_read,
319
+ try:
320
+ if isinstance(response, GroqChatCompletion):
321
+ output, usage_data = _format_groq_output(response)
322
+ # Serialize structured data to JSON for span attribute
323
+ if output:
324
+ if isinstance(output, list):
325
+ output_str = safe_serialize(output)
326
+ else:
327
+ output_str = str(output)
328
+ set_span_attribute(
329
+ span, AttributeKeys.GEN_AI_COMPLETION, output_str
330
+ )
331
+ if usage_data:
332
+ (
333
+ prompt_tokens,
334
+ completion_tokens,
335
+ cache_read,
336
+ cache_creation,
337
+ ) = _extract_groq_tokens(usage_data)
338
+ set_span_attribute(
339
+ span,
340
+ AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
341
+ prompt_tokens,
342
+ )
343
+ set_span_attribute(
344
+ span,
345
+ AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
346
+ completion_tokens,
347
+ )
348
+ set_span_attribute(
349
+ span,
350
+ AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
351
+ cache_read,
352
+ )
353
+ set_span_attribute(
354
+ span,
355
+ AttributeKeys.JUDGMENT_USAGE_METADATA,
356
+ safe_serialize(usage_data),
357
+ )
358
+ # Add groq/ prefix to response model for server-side cost calculation
359
+ response_model = getattr(response, "model", model_name)
360
+ prefixed_response_model = (
361
+ f"groq/{response_model}" if response_model else ""
346
362
  )
347
363
  set_span_attribute(
348
364
  span,
349
- AttributeKeys.JUDGMENT_USAGE_METADATA,
350
- safe_serialize(usage_data),
365
+ AttributeKeys.GEN_AI_RESPONSE_MODEL,
366
+ prefixed_response_model,
351
367
  )
352
- # Add groq/ prefix to response model for server-side cost calculation
353
- response_model = getattr(response, "model", model_name)
354
- prefixed_response_model = (
355
- f"groq/{response_model}" if response_model else ""
356
- )
357
- set_span_attribute(
358
- span,
359
- AttributeKeys.GEN_AI_RESPONSE_MODEL,
360
- prefixed_response_model,
368
+ except Exception as e:
369
+ judgeval_logger.error(
370
+ f"[groq wrapped] Error adding span metadata: {e}"
361
371
  )
362
- return response
372
+ finally:
373
+ return response
363
374
 
364
375
  return wrapper
365
376
 
@@ -388,71 +399,81 @@ def wrap_groq_client(tracer: Tracer, client: GroqClientType) -> GroqClientType:
388
399
  async with async_span_context(
389
400
  tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
390
401
  ) as span:
391
- tracer.add_agent_attributes_to_span(span)
392
- set_span_attribute(
393
- span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
394
- )
395
- model_name = kwargs.get("model", "")
396
- # Add groq/ prefix for server-side cost calculation
397
- prefixed_model_name = f"groq/{model_name}" if model_name else ""
398
- set_span_attribute(
399
- span, AttributeKeys.GEN_AI_REQUEST_MODEL, prefixed_model_name
400
- )
402
+ try:
403
+ tracer.add_agent_attributes_to_span(span)
404
+ set_span_attribute(
405
+ span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
406
+ )
407
+ model_name = kwargs.get("model", "")
408
+ # Add groq/ prefix for server-side cost calculation
409
+ prefixed_model_name = f"groq/{model_name}" if model_name else ""
410
+ set_span_attribute(
411
+ span,
412
+ AttributeKeys.GEN_AI_REQUEST_MODEL,
413
+ prefixed_model_name,
414
+ )
415
+ except Exception as e:
416
+ judgeval_logger.error(
417
+ f"[groq wrapped_async] Error adding span metadata: {e}"
418
+ )
419
+
401
420
  response = await function(*args, **kwargs)
402
421
 
403
- if isinstance(response, GroqChatCompletion):
404
- output, usage_data = _format_groq_output(response)
405
- # Serialize structured data to JSON for span attribute
406
- if output:
407
- if isinstance(output, list):
408
- import orjson
409
-
410
- output_str = orjson.dumps(
411
- output, option=orjson.OPT_INDENT_2
412
- ).decode()
413
- else:
414
- output_str = str(output)
415
- set_span_attribute(
416
- span, AttributeKeys.GEN_AI_COMPLETION, output_str
417
- )
418
- if usage_data:
419
- (
420
- prompt_tokens,
421
- completion_tokens,
422
- cache_read,
423
- cache_creation,
424
- ) = _extract_groq_tokens(usage_data)
425
- set_span_attribute(
426
- span,
427
- AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
428
- prompt_tokens,
429
- )
430
- set_span_attribute(
431
- span,
432
- AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
433
- completion_tokens,
434
- )
435
- set_span_attribute(
436
- span,
437
- AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
438
- cache_read,
422
+ try:
423
+ if isinstance(response, GroqChatCompletion):
424
+ output, usage_data = _format_groq_output(response)
425
+ # Serialize structured data to JSON for span attribute
426
+ if output:
427
+ if isinstance(output, list):
428
+ output_str = safe_serialize(output)
429
+ else:
430
+ output_str = str(output)
431
+ set_span_attribute(
432
+ span, AttributeKeys.GEN_AI_COMPLETION, output_str
433
+ )
434
+ if usage_data:
435
+ (
436
+ prompt_tokens,
437
+ completion_tokens,
438
+ cache_read,
439
+ cache_creation,
440
+ ) = _extract_groq_tokens(usage_data)
441
+ set_span_attribute(
442
+ span,
443
+ AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
444
+ prompt_tokens,
445
+ )
446
+ set_span_attribute(
447
+ span,
448
+ AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
449
+ completion_tokens,
450
+ )
451
+ set_span_attribute(
452
+ span,
453
+ AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
454
+ cache_read,
455
+ )
456
+ set_span_attribute(
457
+ span,
458
+ AttributeKeys.JUDGMENT_USAGE_METADATA,
459
+ safe_serialize(usage_data),
460
+ )
461
+ # Add groq/ prefix to response model for server-side cost calculation
462
+ response_model = getattr(response, "model", model_name)
463
+ prefixed_response_model = (
464
+ f"groq/{response_model}" if response_model else ""
439
465
  )
440
466
  set_span_attribute(
441
467
  span,
442
- AttributeKeys.JUDGMENT_USAGE_METADATA,
443
- safe_serialize(usage_data),
468
+ AttributeKeys.GEN_AI_RESPONSE_MODEL,
469
+ prefixed_response_model,
444
470
  )
445
- # Add groq/ prefix to response model for server-side cost calculation
446
- response_model = getattr(response, "model", model_name)
447
- prefixed_response_model = (
448
- f"groq/{response_model}" if response_model else ""
449
- )
450
- set_span_attribute(
451
- span,
452
- AttributeKeys.GEN_AI_RESPONSE_MODEL,
453
- prefixed_response_model,
471
+ except Exception as e:
472
+ judgeval_logger.error(
473
+ f"[groq wrapped_async] Error adding span metadata: {e}"
454
474
  )
455
- return response
475
+ finally:
476
+ return response
456
477
 
457
478
  return wrapper
458
479