Spaces:
Running on T4
Download AI-coach-agent/docs/SWING_REPORT_API.md from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 16.3 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/docs/SWING_REPORT_API.md
- Command line
-
hf download hf://spaces/modventures/baseball-engine/AI-coach-agent/docs/SWING_REPORT_API.md
-
curl -L -o SWING_REPORT_API.md https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/docs/SWING_REPORT_API.md
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. 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(contract1.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 (seebackend/docs/FRONTEND_INTEGRATION.md§5). With?queue=falsethe call is synchronous: the response is200with the report below and takes a few seconds. The report is also saved on the session, andGET /v1/sessions/{id}returns it asreport. - The backend takes
levelandheight_cmfrom the session's athlete, so the ML payload needs no extra fields.batting_handcomes from the payload'sathlete.batting_handwhen set (it is the side actually analysed), otherwise from the session athlete'sbats(L or R), otherwise from the payload'slead_foot. - Sending the same
analysis_id+analysis_revisionagain 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
benchmarknumbers are the real OBP values computed indata_ref/calibration/contract_v1/. - All other values are illustrative.
{
"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).
wis the width of the satisfactory band on that side: P25 minus P10 below, or P90 minus P75 above.dis 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 statusavailable. 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_targetmeasurement takes priority over asatisfactoryone. focus_areaisnullwhen nothing qualifies. In that caseoverview.headlinesays 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_valueandbenchmark. - Gemini writes
headline,summary,coach_message,result_textandcoach_tip. - Code decides every rating and the focus area.
practice_feelanddrillcome only from the coach's drill library.- If the model fails or breaks a guardrail, fixed-rule text is used instead, and
meta.text_sourceis set torulesormixed. - Since last time. The app backend passes the player's previous report for the same
practice_type(their own reps only) asbuild_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_handandlevel. Separation flips sign between right- and left-handed hitters, so each hand has its own ranges. - The athlete comes from the session snapshot. When
levelis unknown, high-school ranges are used and alevel_assumednote is added todata_quality.youthuses high-school ranges andadultuses college ranges, with alevel_approximatednote. - 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_fallbacknote. - When the batting side is unknown, no measurement is rated (
no_benchmark) and abatting_hand_unknownwarning 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.
{
"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.