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

# Score logs by Topics facet values with batch scoring

export const plans_0 = "Pro, Enterprise"

export const deployments_0 = "Braintrust-hosted, Self-hosted"

export const data_plane_version_0 = undefined

export const use_case_0 = "Use case - Run scoring on production logs filtered by Topics facet values"

<Note>
  **Applies to:**

  * Plan - {plans_0}
  * Deployment - {deployments_0}
  * {data_plane_version_0}
  * {use_case_0}
</Note>

## Summary

**Issue:** Using `facets.*` fields (for example, `facets.Sentiment`) in an online scoring automation SQL filter returns `Error: Unknown field: facets`, or the filter never matches traces.

**Cause:** Online scoring automation SQL filters run when spans complete, but Topics writes `facets` later on a separate span. Online scoring does not re-run when facets are populated.

**Resolution:** Use programmatic batch scoring with SQL and `log_feedback()` after Topics has populated facets.

## Resolution steps

Real-time chaining (Topics automation to scoring automation) is not currently supported. Use the batch scoring approach below instead.

Topics writes `facets` on the Topics automation span, not the root span. Use `ANY_SPAN()` to find matching traces and `FILTER_SPANS(is_root)` to return the root span `id` for `log_feedback()`. Adjust `FILTER_SPANS()` to return the span that has the fields your scorer needs.

### Step 1: Query traces by facet value

```sql theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
SELECT id
FROM project_logs('<PROJECT_ID>', shape => 'traces')
WHERE ANY_SPAN(facets."Feature_category" = 'A')
  AND FILTER_SPANS(is_root)
LIMIT 100
```

### Step 2: Score and attach results

```python theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
import braintrust

logger = braintrust.init_logger(project_id="<PROJECT_ID>")

query = """
SELECT id
FROM project_logs('<PROJECT_ID>', shape => 'traces')
WHERE ANY_SPAN(facets."Feature_category" = 'A')
  AND FILTER_SPANS(is_root)
LIMIT 100
"""

for trace in braintrust.api_conn().post_json("btql", {"query": query})["data"]:
    score = my_scorer(trace)
    logger.log_feedback(id=trace["id"], scores={"my_score": score})

braintrust.flush()
```

Use the span `id` from the query for `log_feedback()` rather than the `root_span_id` or `span_id`.
