# Scanning (/docs/python/sdk/scanning)



Every scan method hits the same underlying detection pipeline with different
**provenance** — a plain prompt, a retrieved document chunk, a tool call, and
so on — and they all return the same `ScanResult` shape (RAG-family methods
add a couple of extra fields; `scan_retrieval` returns a list plus an
aggregate verdict instead). Methods on `AsyncTawaGuard` have the same
signatures as `TawaGuard`, as coroutines.

## `analyze` [#analyze]

The general-purpose entry point. Scans a plain prompt before it reaches your
LLM.

```python
result = client.analyze("Ignore all previous instructions…")
```

### Result [#result]

`ScanResult` is a frozen dataclass:

| Field             | Type                         | Description                                                                                                                                                                                                                                                                                                 |
| ----------------- | ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `action`          | `ScanAction`                 | The action to enforce: `"allow"` or `"block"`. The gateway collapses the engine's `"review"` per your app's setting. Against a gateway that predates `engine_action`, the engine's raw label (any of `"allow" \| "block" \| "soft_block" \| "hard_block" \| "review" \| "sandbox" \| "ambiguous" \| "ood"`) |
| `engine_action`   | `ScanAction \| None`         | The engine's own label before collapsing, for example `"review"`. Falls back to `action` when the gateway does not send it                                                                                                                                                                                  |
| `needs_review`    | `bool`                       | `True` when the engine was uncertain (its action was `"review"`), whether `action` is `"allow"` or `"block"`. `"ood"` does not set it                                                                                                                                                                       |
| `risk`            | `float \| None`              | How likely the input is malicious (0-1), regardless of `action`. `None` for older responses or out-of-distribution input                                                                                                                                                                                    |
| `confidence`      | `float`                      | Confidence in `action` (0-1). Equals `risk` for any flagging action, and `1 - risk` for `allow`                                                                                                                                                                                                             |
| `suggestion`      | `"allow" \| "block" \| None` | Coarse hint for the undecided actions (`ambiguous`/`review`/`sandbox`)                                                                                                                                                                                                                                      |
| `rationale`       | `str`                        | Human-readable explanation of the decision                                                                                                                                                                                                                                                                  |
| `policy`          | `PolicyVerdict \| None`      | Directives signaled by the worker's policy engine, if any                                                                                                                                                                                                                                                   |
| `language`        | `LanguageInfo \| None`       | Language identification detail, `None` while language ID is disabled                                                                                                                                                                                                                                        |
| `score_breakdown` | `dict[str, Any] \| None`     | Diagnostic detail behind the decision — shape varies by which decision path fired                                                                                                                                                                                                                           |

```python
if result.action == "allow":
    print(f"Safe ({result.confidence:.2f} confidence)")
else:
    print(f"Blocked: {result.rationale}")

if result.needs_review:
    # The engine was uncertain; engine_action holds its own label.
    print(f"Flagged for review (engine said {result.engine_action})")
```

See [The review flag](/docs/python/guides/review-flag). Against a gateway that
predates these fields, `needs_review` is derived from `action == "review"` and
`action` carries the engine's raw label, so keep handling `"review"`,
`"soft_block"`, `"sandbox"`, and `"ambiguous"` if you support such a gateway.

### Options [#options]

Every scan method accepts `cuid`, `tenant_id`, `policy_overrides`, and
`language` as keyword arguments, forwarded straight to the worker per call.
`scan_tool_call` and `scan_tool_output` take no `language`.

Every scan method also accepts `session_id`, an opaque id that correlates every
scan in one agent turn (its prompt, tool calls, retrieval and reply). It is
omitted from the request when not set.

<Callout type="info" title="Available from 0.3.0">
  `session_id` on the scan methods is new in 0.3.0.
</Callout>

## RAG protection [#rag-protection]

Scan retrieved content before it's assembled into a prompt.

```python
# A single retrieved chunk, before it enters the assembled context.
chunk_result = client.scan_chunk(chunk_text, chunk_id="doc-42#3")

# The fully assembled prompt (system + retrieved chunks + user turn),
# immediately before the LLM call.
context_result = client.scan_context(assembled_prompt)

# An entire retrieved set in one call — a per-chunk verdict plus an
# aggregate.
from tawaguard import RagChunkInput

result = client.scan_retrieval(
    query,
    [
        RagChunkInput(chunk_id="doc-42#1", text="…"),
        RagChunkInput(chunk_id="doc-42#2", text="…"),
    ],
)
print(result.per_chunk, result.aggregate)

result.aggregate.flagged_chunk_ids  # chunks enforced as block
result.aggregate.review_chunk_ids   # chunks the engine flagged, blocked or not
```

See the [RAG protection cookbook](/docs/python/cookbook/rag-protection) for a
full walkthrough combining all three.

## Agent protection [#agent-protection]

Inspect a proposed tool call before it executes, and its output before it
re-enters the agent loop. If you build on the OpenAI Agents SDK, the
[tool guardrails](/docs/python/integrations/openai-agents-sdk#tool-guardrails)
run both scans for you.

```python
call_result = client.scan_tool_call("run_query", {"sql": "SELECT …"})
if call_result.action != "allow":
    # withhold the tool call
    ...

output_result = client.scan_tool_output("run_query", query_result)
```

See the [Agent protection cookbook](/docs/python/cookbook/agent-protection) for
a full walkthrough with both gates and policy directive handling.

## Scanning a model's reply [#scanning-a-models-reply]

`scan_response` scans the model's own reply before it reaches the end user. A
reply can carry an injection aimed at the next turn, the same threat as a
tool's output, so it is sent with its own provenance (`llm_response`) and
returns the same `ScanResult`, `needs_review` and `engine_action` included. It
is available on `TawaGuard` and `AsyncTawaGuard`.

```python
scan = client.scan_response(reply, session_id=turn_id)
if scan.action != "allow":
    reply = "Sorry, I can't share that."
```

Signature: `scan_response(text, *, cuid=None, tenant_id=None,
policy_overrides=None, language=None, session_id=None)`.

<Callout type="info" title="Available from 0.3.0">
  `scan_response` is new in 0.3.0. It matches `scanResponse` in the
  TypeScript SDK.
</Callout>
