IsJson

class opik.evaluation.metrics.IsJson(name: str = 'is_json_metric', track: bool = True)

Bases: BaseMetric

A metric that checks if a given output string is valid JSON.

This metric returns a score of 1.0 if the output string can be parsed as JSON, and 0.0 otherwise.

Parameters:
  • name – The name of the metric. Defaults to “is_json_metric”.

  • track – Whether to track the metric. Defaults to True.

Example

>>> from opik.evaluation.metrics import IsJson
>>> is_json_metric = IsJson()
>>> result = is_json_metric.score('{"key": "value"}')
>>> print(result.value)
1.0
>>> result = is_json_metric.score('Not a JSON string')
>>> print(result.value)
0.0
score(output: str, **ignored_kwargs: Any) ScoreResult

Calculate the score based on whether the output string is valid JSON.

Parameters:
  • output – The output string to check for JSON validity.

  • **ignored_kwargs – Additional keyword arguments that are ignored.

Returns:

A ScoreResult object with a value of 1.0 if the output

is valid JSON, 0.0 otherwise.

Return type:

score_result.ScoreResult

async ascore(*args: Any, **kwargs: Any) ScoreResult | List[ScoreResult]

Async public method that can be called independently.