Skip to content

MistralClient: NoneType error when structured output validation fails #3

@MHindermann

Description

@MHindermann

MistralClient: NoneType error when structured output validation fails

From RISE-UNIBAS/humanities_data_benchmark#84

Description

When Mistral API returns structured JSON output with None values that fail Pydantic validation, the client logs a warning but leaves parsed_data as None. This causes a 'NoneType' object is not subscriptable error in calling code that expects to access the parsed data.

Error Log

2026-01-24 11:14:07,768 INFO:root:Running T0061
2026-01-24 11:14:07,770 INFO:root:Loaded business_letters from ../benchmarks\business_letters\benchmark.py
2026-01-24 11:14:08,405 INFO:root:Running business_letters (mistral/pixtral-large-2411)...
2026-01-24 11:14:08,437 INFO:root:Found 57 objects to process.
2026-01-24 11:14:08,439 INFO:root:Processing T0061, letter01...
2026-01-24 11:14:08,562 INFO:ai_client.utils:Resized image letter01_p1.jpg from (2479, 3508) to (1447, 2048)
2026-01-24 11:14:08,692 INFO:ai_client.utils:Resized image letter01_p2.jpg from (2479, 3508) to (1447, 2048)
2026-01-24 11:14:08,803 INFO:ai_client.utils:Resized image letter01_p3.jpg from (2479, 3508) to (1447, 2048)
2026-01-24 11:14:10,372 INFO:httpx:HTTP Request: POST https://api.mistral.ai/v1/chat/completions "HTTP/1.1 200 OK"
2026-01-24 11:14:10,375 INFO:root:Saved answer to ..\results\2026-01-24\T0061\request_T0061_letter01.json
2026-01-24 11:14:10,376 INFO:root:Finished letter01 with score: {'send_date_tp': 0, 'send_date_fp': 0, 'send_date_fn': 1, 'sender_persons_tp': 0, 'sender_persons_fp': 0, 'sender_persons_fn': 2, 'receiver_persons_tp': 0, 'receiver_persons_fp': 0, 'receiver_persons_fn': 1}
2026-01-24 11:14:10,376 INFO:root:Processing T0061, letter02...
2026-01-24 11:14:10,487 INFO:ai_client.utils:Resized image letter02_p1.jpg from (2479, 3508) to (1447, 2048)
2026-01-24 11:14:10,595 INFO:ai_client.utils:Resized image letter02_p2.jpg from (2479, 3508) to (1447, 2048)
2026-01-24 11:14:10,700 INFO:ai_client.utils:Resized image letter02_p3.jpg from (2479, 3508) to (1447, 2048)
2026-01-24 11:14:10,840 INFO:ai_client.utils:Resized image letter02_p4.jpg from (2479, 3508) to (1447, 2048)
2026-01-24 11:14:10,953 INFO:ai_client.utils:Resized image letter02_p5.jpg from (2479, 3508) to (1447, 2048)
2026-01-24 11:14:14,191 INFO:httpx:HTTP Request: POST https://api.mistral.ai/v1/chat/completions "HTTP/1.1 200 OK"
2026-01-24 11:14:14,194 WARNING:ai_client.mistral_client:Failed to validate Mistral structured response: 4 validation errors for Document
metadata.send_date.0
Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
  For further information visit https://errors.pydantic.dev/2.10/v/string_type
metadata.letter_title.0
Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
  For further information visit https://errors.pydantic.dev/2.10/v/string_type
metadata.sender_persons.0
Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
  For further information visit https://errors.pydantic.dev/2.10/v/string_type
metadata.receiver_persons.0
Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
  For further information visit https://errors.pydantic.dev/2.10/v/string_type
2026-01-24 11:14:14,194 CRITICAL:root:T0061: 'NoneType' object is not subscriptable

Expected Behavior

When validation fails, parsed_data should still contain the raw JSON response so calling code can access it (even if invalid).

Location

ai_client/mistral_client.py:154-163 in _create_response_from_raw()

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions