# openai.BadRequestError: Invalid training file: 'messages' must be a list of objects, but got <class 'str'> for line 42

- **ID:** `llm/fine-tuning-data-format-invalid`
- **Domain:** llm
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 88%

## Root Cause

A JSONL training file for fine-tuning contains a malformed line where the 'messages' field is a string instead of a list of message objects, often due to a missing comma or incorrect JSON serialization.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| openai==1.30.0 | active | — | — |
| python==3.11 | active | — | — |

## Workarounds

1. **Validate the training file locally before uploading: `import json; with open('training.jsonl') as f: for i, line in enumerate(f, 1): try: obj = json.loads(line); assert isinstance(obj['messages'], list), f'Line {i}: messages is not a list'; for msg in obj['messages']: assert 'role' in msg and 'content' in msg, f'Line {i}: message missing role or content' except (json.JSONDecodeError, AssertionError) as e: print(f'Error on line {i}: {e}')`** (95% success)
   ```
   Validate the training file locally before uploading: `import json; with open('training.jsonl') as f: for i, line in enumerate(f, 1): try: obj = json.loads(line); assert isinstance(obj['messages'], list), f'Line {i}: messages is not a list'; for msg in obj['messages']: assert 'role' in msg and 'content' in msg, f'Line {i}: message missing role or content' except (json.JSONDecodeError, AssertionError) as e: print(f'Error on line {i}: {e}')`
   ```
2. **Use the OpenAI CLI tool to validate the file: `openai api fine_tunes.create -t training.jsonl -v` which provides detailed error messages for each malformed line.** (90% success)
   ```
   Use the OpenAI CLI tool to validate the file: `openai api fine_tunes.create -t training.jsonl -v` which provides detailed error messages for each malformed line.
   ```

## Dead Ends

- **** — The error may be caused by a systemic issue (e.g., incorrect serialization logic) that affects multiple lines; fixing only line 42 won't prevent the same error on other lines. (75% fail)
- **** — Standard JSON validators will pass a string value for 'messages' as valid JSON; the error is a semantic validation that the OpenAI API performs. (90% fail)
- **** — If the code that writes JSONL is buggy, regenerating will produce the same error on the same or different lines. (95% fail)
