# create_eval_optimize_subgraph

> **Function** in `langchain_azure_ai`

📖 [View in docs](https://reference.langchain.com/python/langchain-azure-ai/evaluation/helpers/create_eval_optimize_subgraph)

Build a compiled evaluator-optimizer subgraph.

Creates a LangGraph ``StateGraph`` that implements the
evaluate→refine loop pattern. The returned compiled graph can
be used as a node in a parent graph.

## Signature

```python
create_eval_optimize_subgraph(
    *,
    evaluate_fn: Callable[[Any], dict[str, Any]],
    refine_fn: Callable[[Any], dict[str, Any]],
    should_refine_fn: Callable[[Any], str],
    state_schema: type,
    max_iterations: int = 3,
    accepted_route: str = 'accepted',
    refine_route: str = 'refine',
) -> Any
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `evaluate_fn` | `Callable[[Any], dict[str, Any]]` | Yes | Node function that evaluates the current draft and returns state updates including evaluation results. |
| `refine_fn` | `Callable[[Any], dict[str, Any]]` | Yes | Node function that refines the draft based on evaluation feedback. |
| `should_refine_fn` | `Callable[[Any], str]` | Yes | Routing function that returns *accepted_route* or *refine_route* based on state. |
| `state_schema` | `type` | Yes | The TypedDict class for the subgraph state. |
| `max_iterations` | `int` | No | Safety limit on refinement loops. (default: `3`) |
| `accepted_route` | `str` | No | Route name when evaluation passes. (default: `'accepted'`) |
| `refine_route` | `str` | No | Route name when refinement is needed. (default: `'refine'`) |

## Returns

`Any`

A compiled LangGraph ``StateGraph`` ready to be invoked

---

[View source on GitHub](https://github.com/langchain-ai/langchain-azure/blob/7a6a664d617264df8dcb12fcfb1bacc09f21d504/libs/azure-ai/langchain_azure_ai/evaluation/helpers.py#L51)