> ## 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: Policy Engine

> Policy and Rule - declarative score enforcement.

<Info>
  **Concept:** [Policy Engine](/concepts/policy-engine) | **API:** [Evaluation API](/api-reference/evaluation)
</Info>

A rule fires when its dimension scores **below** the configured threshold. `Policy` and `Rule` are the local, in-code option. Policy can also be configured per-[application](/concepts/applications) in the dashboard and enforced server-side.

<CodeGroup>
  ```python Basic policy theme={null}
  from rail_score_sdk import RailScoreClient, Policy, Rule

  client = RailScoreClient(api_key="YOUR_RAIL_API_KEY")

  policy = Policy(rules=[
      Rule(dimension="safety",      threshold=7.0, action="block"),
      Rule(dimension="fairness",    threshold=6.0, action="flag"),
      Rule(dimension="reliability", threshold=5.0, action="warn"),
  ])

  result = client.eval(content="...", mode="basic", policy=policy)

  print(result.policy_outcome.action)           # "block" | "warn" | "flag" | "allow"
  print(result.policy_outcome.triggered_rules)  # list of triggered Rule objects
  print(result.policy_outcome.blocked)          # True if action == "block"
  ```

  ```python Default policy on client theme={null}
  HEALTHCARE_POLICY = Policy(rules=[
      Rule(dimension="safety",       threshold=8.5, action="block"),
      Rule(dimension="reliability",  threshold=7.5, action="block"),
      Rule(dimension="privacy",      threshold=8.0, action="block"),
      Rule(dimension="transparency", threshold=6.0, action="warn"),
  ])

  client = RailScoreClient(
      api_key="...",
      default_policy=HEALTHCARE_POLICY,
  )

  # Policy applied automatically on every eval call
  result = client.eval(content="...", mode="basic")
  if result.policy_outcome.blocked:
      return "Unable to provide that information."
  ```

  ```python With callbacks theme={null}
  def on_block(outcome):
      log.error("Response blocked", extra={"rules": outcome.triggered_rules})

  def on_flag(outcome):
      queue_for_review(outcome)

  policy = Policy(
      rules=[
          Rule(dimension="safety", threshold=7.0, action="block"),
          Rule(dimension="fairness", threshold=6.0, action="flag"),
      ],
      on_block=on_block,
      on_flag=on_flag,
  )
  ```
</CodeGroup>
