> ## Documentation Index
> Fetch the complete documentation index at: https://docs.responsibleailabs.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Python: Middleware

> RAILMiddleware - किसी भी async LLM function को automatic RAIL evaluation के साथ wrap करें।

<Info>
  **Concept:** [Middleware](/concepts/middleware) | **API:** [Evaluation API](/api-reference/evaluation)
</Info>

`RAILMiddleware` किसी भी async function को wrap करता है जो LLM responses generate करता है, और आपकी LLM call logic को बदले बिना automatic RAIL scoring add कर देता है।

<CodeGroup>
  ```python Basic setup theme={null}
  import asyncio
  from rail_score_sdk import RAILMiddleware

  async def my_llm(messages, **kwargs):
      # आपकी LLM call यहाँ — string return करती है
      return "The LLM generated this response."

  async def main():
      mw = RAILMiddleware(
          api_key="YOUR_RAIL_API_KEY",
          generate_fn=my_llm,
          threshold=7.0,
          policy="block",
          eval_input=True,
          input_threshold=5.0,
      )

      result = await mw.run(
          messages=[
              {"role": "system", "content": "You are a helpful assistant."},
              {"role": "user", "content": "Explain quantum computing."},
          ]
      )
      print(f"Score: {result.score}, Met threshold: {result.threshold_met}")
      print(f"Content: {result.content}")

  asyncio.run(main())
  ```

  ```python Observe mode (no blocking) theme={null}
  mw = RAILMiddleware(
      api_key="YOUR_RAIL_API_KEY",
      generate_fn=my_llm,
      # कोई threshold नहीं — हर response score होता है लेकिन block नहीं होता
  )

  result = await mw.run(messages=[...])
  print(result.score)        # हमेशा मिलता है
  print(result.threshold_met)  # None — कोई threshold set नहीं है
  ```

  ```python Auto-regenerate on fail theme={null}
  mw = RAILMiddleware(
      api_key="YOUR_RAIL_API_KEY",
      generate_fn=my_llm,
      threshold=7.0,
      policy="regenerate",
      max_iterations=3,
  )

  result = await mw.run(messages=[...])
  print(result.content)           # सभी iterations में से best content
  print(result.iterations_taken)  # 1 अगर original pass हो गया
  ```
</CodeGroup>

## Parameters

| Parameter         | Type             | Default   | Description                            |
| ----------------- | ---------------- | --------- | -------------------------------------- |
| `api_key`         | `str`            | —         | RAIL API key                           |
| `generate_fn`     | `async callable` | —         | आपका LLM function                      |
| `threshold`       | `float`          | `None`    | इस score से नीचे block/regenerate करें |
| `policy`          | `str`            | `"block"` | `"block"` या `"regenerate"`            |
| `eval_input`      | `bool`           | `False`   | Input messages को भी score करें        |
| `input_threshold` | `float`          | `None`    | Input scoring के लिए threshold         |
| `max_iterations`  | `int`            | `3`       | Maximum regeneration attempts          |
