LangChain ChatOpenAI with OpenRouter: structured output and reasoning errors
Fix JSONDecodeError, missing reasoning content, and unsupported parse method when using LangChain's ChatOpenAI with OpenRouter or similar proxies.
The symptom
You are using LangChain's ChatOpenAI class pointed at a proxy such as OpenRouter, and you hit errors when using structured output or reasoning models. The most common error is a JSONDecodeError when calling with_structured_output, even with include_raw=True. The raw response is inaccessible on error, so you cannot inspect what the proxy returned.
Another symptom is that structured output via create_agent fails because the proxy does not support the OpenAI SDK's beta.chat.completions.parse method. You may see an error like 'AttributeError: 'ChatOpenAI' object has no attribute 'beta'' or a 404 from the proxy.
For reasoning models, the reasoning content is missing from the response. You get only the final answer, not the chain-of-thought or reasoning tokens that the model produced.
Why it happens
Ranked root causes:
1. Proxy returns malformed or incomplete JSON for structured output. Confirm by logging the raw HTTP response from the proxy before LangChain parses it. If the JSON is truncated or has trailing commas, that is the cause.
2. Proxy does not implement the beta.chat.completions.parse endpoint. Confirm by checking the proxy's documentation or by making a direct HTTP request to that endpoint and seeing a 404 or 400.
3. Proxy returns reasoning content in a non-standard field that ChatOpenAI does not read. Confirm by inspecting the raw response JSON for fields like reasoning_details or reasoning_content that are not mapped to LangChain's message content.
4. ChatOpenAI is not designed to handle provider-specific extensions. Confirm by checking the LangChain issue tracker for known limitations with your proxy.
How to fix it
- For structured output failures, avoid using with_structured_output with proxies that do not support the parse method. Instead, use a Pydantic parser manually: send a prompt that instructs the model to output JSON, then parse the response with PydanticOutputParser and handle JSONDecodeError by retrying or falling back.
- If you need structured output via create_agent, switch to a provider-specific integration if available, or use a gateway that normalizes the API to match OpenAI's spec.
- For reasoning content, check if the proxy returns reasoning in a field like reasoning_details. If so, you can subclass ChatOpenAI to override _create_chat_result and extract that field into additional_kwargs or response_metadata.
- As a workaround, try langchain-litellm which may handle proxy differences better, but verify that it works for your specific proxy and model.
- Always log the raw response when debugging. Set logging level to DEBUG for the HTTP client to see what the proxy returns.
When this is not the cause
If you are using the official OpenAI API directly, these issues are unlikely. The problems described here are specific to proxies that deviate from the OpenAI spec.
If you are using a proxy that fully implements the OpenAI API including beta endpoints, then structured output should work. Verify by testing with a simple structured output call and checking the raw response.
If the error is a connection timeout or authentication failure, that is a different issue. Check your API key and network connectivity.
How to apply this with Admesh
Admesh provides an OpenAI-compatible base URL at https://api.admesh.ai/v1 and an Anthropic-compatible endpoint at https://api.admesh.ai/v1/messages. You can use ChatOpenAI with Admesh by setting base_url to https://api.admesh.ai/v1 and providing your Admesh API key. Because Admesh normalizes responses across multiple model families, structured output and reasoning content are handled consistently.
For reasoning models, Admesh returns reasoning content in a standard field that ChatOpenAI can capture. You do not need to subclass or patch LangChain.
To get started, sign up at https://console.admesh.ai, create an API key, and check the model catalog at https://admesh.ai/models for available models. Use one key and one balance across OpenAI, Anthropic, Google, DeepSeek, Qwen, Moonshot, Zhipu, and MiniMax models.
FAQ
Why does with_structured_output fail with JSONDecodeError when using OpenRouter?
Can I use ChatAnthropic with OpenRouter?
How do I capture reasoning content from DeepSeek via OpenRouter in LangChain?
Is langchain-litellm a reliable workaround for OpenRouter?
References
Call these models through one gateway
Admesh provides a unified OpenAI-compatible API: one Base URL and one key for Claude, GPT, DeepSeek and more. New accounts get trial credit that never expires.
