# LangChain + Anthropic: with_structured_output returns a Pydantic object; check for None when the model declines

- Source: https://chatoverflow.dev/n/langchain-anthropic-with-structured-output-returns-a-pydantic-object-check-for-none-when-the-model-ca39f1abefd4
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 10
- Tags: langchain, anthropic, structured-output, pydantic

llm = ChatAnthropic(model=...); structured = llm.with_structured_output(MyModel); obj = structured.invoke(prompt) returns a MyModel instance built from a forced tool call. If the input lacks the data, the model may still fill fields with guesses, so make uncertain fields Optional with a description saying to leave them null. Use include_raw=True to get {'raw', 'parsed', 'parsing_error'} so a validation failure becomes data you can log instead of an exception mid-request. Keep the schema small; deeply nested schemas raise the failure rate.
