Spaces:
Running on T4
Running on T4
File size: 16,289 Bytes
2756f0f ae2c68f 2756f0f be69d1e 2756f0f 1b49b17 2756f0f e980186 2756f0f 6437539 2756f0f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | # Swing report API: expected output
**Status:** historical proposal for version `1.0`. This document's sample scores and limited-value aggregation are superseded by the [implemented 1.1 contract and scoring behavior](../../docs/motion-coach-v1.1.md). The receiver accepts both input versions; the report retains version `1.0` with additive interval fields.
**Input:** one swing analysis payload, following the ML engineer's contract v1.0.
**Output:** one JSON swing report. It holds an overall score, a score and rating for each group, the focus area, the coach message, a practice cue and per-measurement explanations.
The report contains no video, frames or keypoints. It does not point at or highlight anything in the video.
## Call
The app calls this endpoint after it has received the payload from the ML API. The session must already exist; see `backend/DESIGN.md`.
```
POST /v1/sessions/{session_id}/swing-report
Content-Type: application/json
Authorization: Bearer <access_token>
```
- The request body is the ML API's `SwingAnalysisPayload` (contract `1.0`), unchanged.
- By default the report is queued: the response is `202` `{session_id, status}` and the owner is notified when the report is saved (see `backend/docs/FRONTEND_INTEGRATION.md` §5). With `?queue=false` the call is synchronous: the response is `200` with the report below and takes a few seconds. The report is also saved on the session, and `GET /v1/sessions/{id}` returns it as `report`.
- The backend takes `level` and `height_cm` from the session's athlete, so the ML payload needs no extra fields. `batting_hand` comes from the payload's `athlete.batting_hand` when set (it is the side actually analysed), otherwise from the session athlete's `bats` (L or R), otherwise from the payload's `lead_foot`.
- Sending the same `analysis_id` + `analysis_revision` again returns the stored report, with no new model call.
- An older revision is refused with `409`.
## Response: `200 OK`
The example uses a fictional right-handed college hitter.
- The `benchmark` numbers are the real OBP values computed in `data_ref/calibration/contract_v1/`.
- All other values are illustrative.
```json
{
"schema_version": "1.0",
"analysis_id": "analysis_001",
"analysis_revision": 1,
"overall_score": 55,
"overview": {
"headline": "Your stride is the area to work on. Your shoulders start turning open before your front foot lands."
},
"groups": [
{
"group": "balance_load",
"display_name": "Balance and Load",
"score": null,
"rating": "not_rated",
"rating_label": "Not measured yet",
"not_rated_reason": "no_measurements_available",
"summary": "We do not measure this part of the swing yet.",
"metric_ids": []
},
{
"group": "stride",
"display_name": "Stride",
"score": 55,
"rating": "off_target",
"rating_label": "Off Target",
"not_rated_reason": null,
"summary": "Your step length looks typical, but your shoulders open before your front foot lands.",
"metric_ids": ["hip_shoulder_separation_at_foot_down", "stride_relative_length"]
},
{
"group": "power_move",
"display_name": "Power Move",
"score": null,
"rating": "not_rated",
"rating_label": "Not measured yet",
"not_rated_reason": "no_measurements_available",
"summary": "We do not measure this part of the swing yet.",
"metric_ids": []
},
{
"group": "contact_follow_through",
"display_name": "Contact and Follow-Through",
"score": null,
"rating": "not_rated",
"rating_label": "Not measured",
"not_rated_reason": "measurements_unavailable",
"summary": "We could not confirm the moment the bat met the ball, so this part was not measured.",
"metric_ids": ["hip_shoulder_separation_at_contact"]
}
],
"focus_area": {
"metric_id": "hip_shoulder_separation_at_foot_down",
"group": "stride",
"display_name": "Hip Shoulder Separation at Foot Down",
"rating": "off_target",
"rating_label": "Off Target",
"coach_message": "Your shoulders start turning toward the pitcher before your front foot lands. Keeping them closed a little longer lets your hips lead and stores more energy for the swing.",
"practice_feel": "Keep your front shoulder pointed at the pitcher until your front foot is down.",
"drill": {
"drill_id": "hit_13",
"name": "Front Shoulder In Tee",
"how": "Set the tee on the outer half. Keep your front shoulder pointed at the pitcher until your hips have turned, then swing.",
"cue": "Shoulder in.",
"reps": "3x8"
},
"evidence_metric_ids": ["hip_shoulder_separation_at_foot_down"]
},
"measurements": [
{
"metric_id": "hip_shoulder_separation_at_foot_down",
"display_name": "Hip Shoulder Separation at Foot Down",
"group": "stride",
"status": "available",
"value": 3,
"unit": "deg",
"display_value": "3°",
"score": 55,
"rating": "off_target",
"rating_label": "Off Target",
"rating_is_tentative": false,
"not_rated_reason": null,
"benchmark": {
"benchmark_id": "obp-contract-v1.hip_shoulder_separation_at_foot_down.right.college",
"version": "2026-10-21.1",
"population": "College right-handed hitters (50 athletes)",
"on_target": { "lower": -25.3, "upper": -8.5 },
"satisfactory": { "lower": -32.5, "upper": -0.4 },
"unit": "deg",
"source_reference": "OpenBiomechanics Project hitting dataset (Driveline), percentiles P25-P75 and P10-P90"
},
"what_it_means": "When your front foot lands, how differently your hips and shoulders are pointing.",
"result_text": "Your shoulders were already turning open when your front foot landed.",
"coach_tip": "Let your hips start the turn while your shoulders stay closed.",
"limitations": []
},
{
"metric_id": "stride_relative_length",
"display_name": "Stride Length",
"group": "stride",
"status": "limited",
"value": 24,
"unit": "cm",
"display_value": "24 cm",
"score": 100,
"rating": "on_target",
"rating_label": "On Target",
"rating_is_tentative": true,
"not_rated_reason": null,
"benchmark": {
"benchmark_id": "obp-contract-v1.stride_relative_length.right.college",
"version": "2026-10-21.1",
"population": "College right-handed hitters (50 athletes)",
"on_target": { "lower": 23.8, "upper": 41.9 },
"satisfactory": { "lower": 19.1, "upper": 47.6 },
"unit": "cm",
"source_reference": "OpenBiomechanics Project hitting dataset (Driveline), percentiles P25-P75 and P10-P90"
},
"what_it_means": "How far your front foot steps forward.",
"result_text": "Your step length is in the typical range.",
"coach_tip": null,
"limitations": [
{
"code": "scale_is_model_estimated",
"text": "Distance is estimated from the video, so treat this as approximate."
}
]
},
{
"metric_id": "hip_shoulder_separation_at_contact",
"display_name": "Hip Shoulder Separation at Contact",
"group": "contact_follow_through",
"status": "unavailable",
"value": null,
"unit": "deg",
"display_value": null,
"score": null,
"rating": "not_rated",
"rating_label": "Not measured",
"rating_is_tentative": false,
"not_rated_reason": "measurement_unavailable",
"benchmark": null,
"what_it_means": "When the bat meets the ball, how differently your hips and shoulders are pointing.",
"result_text": null,
"coach_tip": null,
"limitations": [
{
"code": "contact_not_verified",
"text": "We could not confirm the moment the bat met the ball."
}
]
}
],
"data_quality": [
{
"code": "contact_not_verified",
"severity": "info",
"text": "Contact could not be confirmed, so contact measurements are not shown."
},
{
"code": "scale_is_model_estimated",
"severity": "info",
"text": "Distances are estimated from the video and are approximate."
}
],
"meta": {
"generated_at": "2026-10-21T14:30:00Z",
"request_id": "req_4b1d9e",
"text_source": "llm",
"model": "gemini-3.1-flash-lite",
"prompt_version": "swing-report-v1-3a9f12c0",
"cache_hit": false,
"fallback_reason": null,
"input_schema_version": "1.0",
"measurement_definitions_version": "1.0",
"benchmark_registry_version": "2026-10-21.1",
"coaching_rules_version": "1.0"
}
}
```
## Rules behind the fields
### Score (0 to 100, calculated by code, never by the model)
A measurement's score depends on how far its value sits outside the on-target interval (P25 to P75).
- `w` is the width of the satisfactory band on that side: P25 minus P10 below, or P90 minus P75 above.
- `d` is the distance outside the on-target interval. It is 0 when the value is inside.
| Where the value is | Score |
|---|---|
| Inside P25 to P75 (`d = 0`) | 100 |
| Between P25/P75 and P10/P90 (`d <= w`) | `100 - 30 * d / w`, which reaches 70 at P10 or P90 |
| Beyond P10/P90 (`d > w`) | `70 - 70 * (d - w) / (2 * t)`, with a floor of 0, where `t = max(w, 0.45 * (P75 - P25))` |
`t` keeps the fall-off from collapsing when P10 is almost equal to P25 (or P90 to P75): without it a value two degrees off target scored 0. `0.45 * (P75 - P25)` is the P75 to P90 gap of a normal distribution with the same P25 to P75, so bands of ordinary shape are unchanged.
Scores are rounded to whole numbers (half up). A value outside P25 to P75 never scores more than 99, and a value outside P10 to P90 never scores more than 69, so the rating read from the score always matches where the value sits. Group and overall means follow the same rule: 100 only when every score is 100.
- **Measurement score:** as above. It is null when the measurement is not rated.
- **Group `score`:** the mean of the scores of its rated measurements with status `available`. A limited measurement shows its own score but is left out of group and overall scores, because the contract does not allow a limited value to be turned into a precise score. The group score is null when nothing qualifies.
- **`overall_score`:** the mean of the group scores that are not null. Every group counts equally. It is null when no group has a score.
Worked example: separation at foot down is 3 degrees, so `d = 11.5`. The upper `w` is `-0.4 - (-8.5) = 8.1`, giving a score of `70 - 70 * 3.4 / 16.2`, which rounds to 55. The stride group score is 55, because stride length is limited and left out. `overall_score` is also 55, since stride is the only group with a score.
### Rating
The rating follows from the score, so the two always agree.
| `rating` | `rating_label` | Score |
|---|---|---|
| `on_target` | On Target | 100, meaning the value is inside P25 to P75 |
| `satisfactory` | Satisfactory | 70 to 99, meaning the value is inside P10 to P90 |
| `off_target` | Off Target | Below 70, meaning the value is outside P10 to P90 |
| `not_rated` | Not measured / Not rated | Score is null. `not_rated_reason` explains why. |
`not_rated_reason` takes one of these values:
| Value | Used on | Meaning |
|---|---|---|
| `measurement_unavailable` | measurement | The measurement has no value. |
| `no_benchmark` | measurement | No approved range exists for this metric, side and level. |
| `no_measurements_available` | group | The group has no registered metrics yet. |
| `measurements_unavailable` | group | The group has metrics, but none of them had a value. |
| `no_rated_measurements` | group | The group has values, but none can be scored precisely (only limited or unbenchmarked results). Label: Not rated. |
### Group rating
A group's rating comes from its group score, using the table above. It is `not_rated` when the group score is null.
### Limited measurements
- A limited measurement is still scored and rated, with `rating_is_tentative: true`.
- Its limitation text is always included.
- It is left out of group and overall scores.
- It is never chosen as the `focus_area`.
### Focus area
- The focus area is the single measurement furthest outside its range, chosen only among measurements with status `available`.
- An `off_target` measurement takes priority over a `satisfactory` one.
- `focus_area` is `null` when nothing qualifies. In that case `overview.headline` says so.
### Completeness
- All 4 groups are always present, in this order: `balance_load`, `stride`, `power_move`, `contact_follow_through`.
- Every registered measurement is always present, including unavailable ones.
### Text
- All text is plain text with no numbers. Numbers appear only in `value`, `display_value` and `benchmark`.
- Gemini writes `headline`, `summary`, `coach_message`, `result_text` and `coach_tip`.
- Code decides every rating and the focus area.
- `practice_feel` and `drill` come only from the coach's drill library.
- If the model fails or breaks a guardrail, fixed-rule text is used instead, and `meta.text_source` is set to `rules` or `mixed`.
- **Since last time.** The app backend passes the player's previous report for the same `practice_type` (their own reps only) as `build_report(..., previous=PreviousSession(report, days_ago))`. The model is then told, in words only, when that session was and whether each result moved closer to the typical range, further from it or stayed about the same (a change of 10 score points or more counts as a move). The request and response shapes do not change; without a previous report the text never mentions earlier sessions.
### Benchmark selection
- The benchmark is chosen by the athlete's `batting_hand` and `level`. Separation flips sign between right- and left-handed hitters, so each hand has its own ranges.
- The athlete comes from the session snapshot. When `level` is unknown, high-school ranges are used and a `level_assumed` note is added to `data_quality`. `youth` uses high-school ranges and `adult` uses college ranges, with a `level_approximated` note.
- When the chosen population has fewer than 5 athletes or no data (left-handed high school today), the same hand's college ranges are used with a `population_fallback` note.
- When the batting side is unknown, no measurement is rated (`no_benchmark`) and a `batting_hand_unknown` warning is added.
### App display
Colors come from `rating` alone; the app should not recompute anything.
| `rating` | Color |
|---|---|
| `on_target` | green |
| `satisfactory` | yellow |
| `off_target` | red |
| `not_rated` | grey |
## Error response
Errors use the backend's single error envelope. `fields` maps each payload path to its problem.
```json
{
"error": {
"code": "INVALID_PAYLOAD",
"message": "The payload does not follow swing analysis contract 1.0.",
"fields": {
"measurements[1].reasons": "A limited result needs at least one reason code.",
"events.front_foot_down.video_time_s": "Must be before contact when both are available."
}
}
}
```
| HTTP | `error.code` | When |
|---|---|---|
| 401 | `UNAUTHENTICATED` | The access token is missing or expired. |
| 402 | `PLAN_LIMIT` | The free plan's 20 analyses this week are used up. |
| 403 | `FORBIDDEN` | The session belongs to someone else. |
| 404 | `NOT_FOUND` | The session does not exist or was deleted. |
| 409 | `STALE_REVISION` | The payload is an older revision than one already reported on. |
| 409 | `ANALYSIS_MISMATCH` | The session already has a report for a different `analysis_id`. |
| 413 | `TOO_LARGE` | The body is too large. |
| 422 | `INVALID_PAYLOAD` | The payload breaks a contract rule. `fields` lists each issue. |
| 422 | `MODE_NOT_SUPPORTED` | The session is a pitching session. Only hitting is supported for now. |
| 503 | `UNAVAILABLE` | A temporary problem. Retry with backoff. |
## Open dependency
The separation-at-foot-down benchmark depends on how `front_foot_down` is defined.
- The example above uses first touch.
- If the backend detects full plant instead, the ranges move by about 15 degrees: the right-handed median goes from -20 to -35.
|