# Python Code Metrics

## Creating a Python-Based Metric in Dynamiq: A Quick Guide

<figure><img src="/files/8iaPny4iwPEzOKaffSKS" alt=""><figcaption></figcaption></figure>

### The Power of Custom Python Metrics

One of Dynamiq's most powerful features is the ability to create custom Python-based metrics. This functionality gives you unparalleled flexibility in evaluating your AI workflows. Rather than being limited to pre-defined evaluation criteria, you can implement exactly the logic you need to assess your specific use cases.

### Creating Your Python Metric

To create a Python metric in Dynamiq:

1. Navigate to **Evaluations → Metrics → Create new Metric**
2. Select the **Python** tab

When you arrive at the Python tab, you'll see an example metric to help you get started. For even faster development, you can choose from several pre-built templates:

* Exact Match
* Email Presence
* Phone Presence
* String Presence
* Arithmetic Sum
* JSON Validity Check

### How Python Metrics Work

Let's look at a simple example - the "Exact Match" metric:

```python
def evaluate(answer: str, expected: str) -> int:
    if answer == expected:
        score = 1
    else:
        score = 0
    return score
```

This code checks if an answer exactly matches the expected value, returning a score of 1 for a match or 0 otherwise.

#### Important Note on Function Arguments

When you define a Python metric, Dynamiq automatically extracts the function arguments (in this case, `answer` and `expected`). During evaluation runs, you'll need to map these arguments to specific fields from your dataset or workflow output.

This mapping system allows your metrics to dynamically access the relevant data during evaluation.

<figure><img src="/files/ixZBCoUu6Oaagl1xqyY8" alt="" width="375"><figcaption></figcaption></figure>

<figure><img src="/files/dmHlATPmJm2kGbFwuVm8" alt=""><figcaption></figcaption></figure>

### Technical Implementation Details

Under the hood, Dynamiq uses RestrictedPython to safely execute your metric code. This means some operations may be prohibited for security reasons.

For a detailed overview of allowed Python libraries and implementation details, you can review the [official Dynamiq GitHub repository](https://github.com/dynamiq-ai/dynamiq/blob/main/dynamiq/nodes/tools/python.py).

### Taking Your Evaluations to the Next Level

With custom Python metrics at your fingertips, you can now create sophisticated evaluation pipelines tailored to your exact requirements. This functionality enables you to:

* Implement domain-specific evaluation logic
* Create composite metrics that consider multiple factors
* Evaluate complex data structures and relationships
* Build metrics that align perfectly with your business objectives

By leveraging the full power of Python within your evaluation pipeline, you can gain deeper insights into your AI applications and make data-driven improvements with confidence.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.getdynamiq.ai/old-version-evaluations/python-code-metrics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
