# Claude-sonnet-5 and chat.with\_structured\_output issues

**URL:** <https://forum.langchain.com/t/claude-sonnet-5-and-chat-with-structured-output-issues/4213>\
**Category:** LangChain\
**Tags:** python-help\
**Created:** [July 21, 2026, 7:55pm UTC](https://forum.langchain.com/t/claude-sonnet-5-and-chat-with-structured-output-issues/4213 "2026-07-21T19:55:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![darthShana](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@darthShana](https://forum.langchain.com/u/darthShana)\
**Post date:** [July 21, 2026, 7:55pm UTC](https://forum.langchain.com/t/claude-sonnet-5-and-chat-with-structured-output-issues/4213/1 "2026-07-21T19:55:12Z")

</div>

i recently switched to try

```auto
claude-sonnet-5

```

and now i getting intermittent issues with

```auto
class Classification(BaseModel):
    transaction_id: str = Field(description="transaction id")
    transaction_type: str = Field(description="classification of the transaction")
    reasoning: str = Field(description="reason of the classification, including any applicable special instructions")

class Classifications(BaseModel):
    classifications: list[Classification] = Field(description="list of transaction classifications")

        structured_llm = self.chat.with_structured_output(Classifications)

```

while im expecting a Classification object from the call the llm seems to return a structure that’s can be transformed into the pydantic object due to double enveloping response.

```auto
[{"type":"tool_call","id":"toolu_011fM8mzebeLLaxUPRDccHqa","name":"Classifications","args":{"classifications":"{\"classifications\": [{\"transaction_id\": \"5491661-01-20241205\", \"transaction_type\": \"water\", \"reasoning\": \"This transaction represents a water bill invoice for the property at 34 Karoro Rd Flat Bush, including water consumption, wastewater consumption, and wastewater fixed charges. Although the amount is shown as positive, the details indicate this is a water utility invoice due to be paid (due_date 27/12/2024), consistent with a payment for water services rather than income.\"}]}"}}]

```

classification is double enveloped, causing

```auto
1 validation error for Classifications
classifications
  Input should be a valid list [
  type=list_type,
  input_value='{"classifications": [{"t...rather than income."}]}',
  input_type=str
]
    For further information visit https://errors.pydantic.dev/2.12/v/list_typeTraceback (most recent call last):

  File "/root/.cache/pypoetry/virtualenvs/in-the-black-47xJI4oC-py3.12/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 2060, in _call_with_config
    context.run(

  File "/root/.cache/pypoetry/virtualenvs/in-the-black-47xJI4oC-py3.12/lib/python3.12/site-packages/langchain_core/runnables/config.py", line 461, in call_func_with_variable_args
    return func(input, **kwargs) # type: ignore[call-arg]
           ^^^^^^^^^^^^^^^^^^^^^

  File "/root/.cache/pypoetry/virtualenvs/in-the-black-47xJI4oC-py3.12/lib/python3.12/site-packages/langchain_core/output_parsers/base.py", line 208, in <lambda>
    lambda inner_input: self.parse_result(
                        ^^^^^^^^^^^^^^^^^^

  File "/root/.cache/pypoetry/virtualenvs/in-the-black-47xJI4oC-py3.12/lib/python3.12/site-packages/langchain_core/output_parsers/openai_tools.py", line 369, in parse_result
    pydantic_objects.append(
  tool(
    **res[
      "args"
    ]
  )
)
                            ^^^^^^^^^^^^^^^^^^^

  File "/root/.cache/pypoetry/virtualenvs/in-the-black-47xJI4oC-py3.12/lib/python3.12/site-packages/pydantic/main.py", line 250, in __init__
    validated_self = self. __pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

pydantic_core._pydantic_core.ValidationError: 1 validation error for Classifications
classifications
  Input should be a valid list [
  type=list_type,
  input_value='{"classifications": [{"t...rather than income."}]}',
  input_type=str
]
    For further information visit https://errors.pydantic.dev/2.12/v/list_type

```

---

<div class="post-metadata">

**Author:** ![dariel.datoon](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/dariel.datoon/32/3152_2.png) [@dariel.datoon](https://forum.langchain.com/u/dariel.datoon)\
**Post date:** [July 21, 2026, 8:53pm UTC](https://forum.langchain.com/t/claude-sonnet-5-and-chat-with-structured-output-issues/4213/2 "2026-07-21T20:53:36Z")

</div>

Hi @darthShana!

Yeah, looks like Claude occasionally returns the nested field as a JSON string instead of a real array.

Have you tried the `json_schema` method?

```python
structured_llm = self.chat.with_structured_output(Classifications, method="json_schema")

```

It’s generally more reliable than the default tool calling method, so I’d give it a try. You’ll just want a recent `langchain-anthropic` installed.

More detail in the docs: [ChatAnthropic integration - Docs by LangChain](https://docs.langchain.com/oss/python/integrations/chat/anthropic#structured-output)

Hope that helps!

---

<div class="post-metadata">

**Author:** ![darthShana](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@darthShana](https://forum.langchain.com/u/darthShana)\
**Post date:** [July 21, 2026, 9:07pm UTC](https://forum.langchain.com/t/claude-sonnet-5-and-chat-with-structured-output-issues/4213/3 "2026-07-21T21:07:13Z")

</div>

> [@dariel.datoon](#):
>
> [ChatAnthropic integration - Docs by LangChain](https://docs.langchain.com/oss/python/integrations/chat/anthropic?%5C_gl=1%5C*1iw7lmo%5C*%5C_gcl_au%5C*MTgyNTQ5OTc0My4xNzgxNjUwODg3%5C*%5C_ga%5C*NTcxOTE2NjU5LjE3ODQ0OTcwNjc.%5C*%5C_ga_47WX3HKKY2%5C*czE3ODQ2NjY1MzAkbzgkZzEkdDE3ODQ2Njc5MTMkajQ3JGwwJGgw#structured-output)

hi @dariel.datoon

thanks for your response.. I did try this, and i found that it ignored a lot of the descriptions in the pydantic description

```auto
    account_number: str = Field(description="the bank account number of the transaction")

```

so it ignored the instructions “the bank account number of the transaction” have you found this to be the case?

---

<div class="post-metadata">

**Author:** ![dariel.datoon](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/dariel.datoon/32/3152_2.png) [@dariel.datoon](https://forum.langchain.com/u/dariel.datoon)\
**Post date:** [July 21, 2026, 9:22pm UTC](https://forum.langchain.com/t/claude-sonnet-5-and-chat-with-structured-output-issues/4213/4 "2026-07-21T21:22:03Z")

</div>

Typically, I’ve seen the model honor the description. What version of `anthropic` and `langchain-anthropic` are you on?

---

<div class="post-metadata">

**Author:** ![darthShana](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@darthShana](https://forum.langchain.com/u/darthShana)\
**Post date:** [July 21, 2026, 9:51pm UTC](https://forum.langchain.com/t/claude-sonnet-5-and-chat-with-structured-output-issues/4213/5 "2026-07-21T21:51:22Z")

</div>

> [@dariel.datoon](#):
>
> langchain-anthropic

im using

**anthropic** -0.84.0

and

langchain\_ **anthropic** -1.3.4

---

<div class="post-metadata">

**Author:** ![dariel.datoon](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/dariel.datoon/32/3152_2.png) [@dariel.datoon](https://forum.langchain.com/u/dariel.datoon)\
**Post date:** [July 21, 2026, 10:20pm UTC](https://forum.langchain.com/t/claude-sonnet-5-and-chat-with-structured-output-issues/4213/6 "2026-07-21T22:20:31Z")

</div>

Thanks for the versions!

I ran some tests on `anthropic` 0.117.0 and `langchain-anthropic` 1.5.0 and couldn’t reproduce it. The field descriptions came through to the model fine.

Do you have a minimal repro of a failing case? Or if you’re tracing in LangSmith, any chance you could share the trace? Either one would help me see what’s different in your setup.

In the meantime, one option is to go back to function calling (drop `method="json_schema"`) and add a small guard: if a field comes back as a string, parse it as JSON before you validate. Something like this, reusing your `Classifications` model:

```python
import json

def coerce_args(args: dict) -> dict:
    """Repair Claude's occasional stringified / double-enveloped tool args."""
    fixed = {}
    for key, value in args.items():
        if isinstance(value, str):
            try:
                decoded = json.loads(value) # field came back as a JSON string
            except (json.JSONDecodeError, ValueError):
                fixed[key] = value # genuinely a string, leave it
                continue
            if isinstance(decoded, dict) and key in decoded:
                decoded = decoded[key] # unwrap the repeated wrapper key
            fixed[key] = decoded
        else:
            fixed[key] = value
    return fixed

# force the tool call, then validate through the guard
llm = self.chat.bind_tools([Classifications], tool_choice="Classifications")

def classify(prompt: str) -> Classifications:
    msg = llm.invoke(prompt)
    return Classifications(**coerce_args(msg.tool_calls[0]["args"]))

```

The guard walks the tool-call args, and for anything that came back as a JSON string it decodes it (and unwraps the repeated key if the model doubled it up). Well-formed responses pass straight through.

---

<div class="post-metadata">

**Author:** ![darthShana](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@darthShana](https://forum.langchain.com/u/darthShana)\
**Post date:** [July 21, 2026, 10:43pm UTC](https://forum.langchain.com/t/claude-sonnet-5-and-chat-with-structured-output-issues/4213/7 "2026-07-21T22:43:41Z")

</div>

thanks @dariel.datoon

ill follow this pattern
