3.2

init_chat_model and model profiles

init_chat_model turns a provider:model string into a chat model of the provider package, and that package, not LangChain, decides how often a failed call is retried.

You pass "qwen3:4b" to create_agent and get a ValueError before the first request. Or you set a client timeout of 15 s, and a call that always times out returns after 46.3 s. Both come from how a model string resolves and which layer retries.

When you finish this section, you can write a model string that resolves and read the profile of a model. You can also count the attempts of one failed model call.

Model strings

Model string: the text form of a model, such as openai:gpt-4.1, that init_chat_model and create_agent accept in place of a model object.

The documentation gives the form as the provider, a colon, and the model, such as openai:o1. init_chat_model splits the string at the first colon only when the part before it is one of 30 known providers. It then imports the class from the provider package:

outputthree model strings and the class each one builtFull log
openai:gpt-4.1 -> langchain_openai.chat_models.base.ChatOpenAI
…
anthropic:claude-sonnet-4-6 -> langchain_anthropic.chat_models.ChatAnthropic
…
ollama:qwen3:4b -> langchain_ollama.chat_models.ChatOllama
Cut to the three result lines. The profiles between them are cut.

With no known prefix, init_chat_model guesses the provider from the start of the name. gpt- and o3 mean OpenAI, and claude means Anthropic. A name that matches no rule raises at once:

outputa name with no known providerFull log
init_chat_model('qwen3:4b'):

raised builtins.ValueError: Unable to infer model provider for model='qwen3:4b'. Please specify 'model_provider' directly.
The list of providers and the help line are cut.

So ollama:qwen3:4b works and qwen3:4b does not. The colon inside an Ollama tag is part of the model name, because only the first colon splits. Figure 3.3 draws the three paths.

Fig. 3.3how init_chat_model resolves a model stringdecision
How init_chat_model resolves a model string A decision figure with three tests in order. First, is the part before the first colon a known provider? openai:gpt-4.1 and ollama:qwen3:4b say yes and build ChatOpenAI and ChatOllama. Second, does the name start with a known prefix such as gpt-, o3, or claude? gpt-4.1-mini and claude-haiku-4-5 say yes and build ChatOpenAI and ChatAnthropic. Third, any other string such as qwen3:4b raises ValueError: Unable to infer model provider, before any request. known provider prefix? the part before the first colon ollama:qwen3:4b ChatOllama, model qwen3:4b known name start? gpt-, o1, o3, claude, gemini claude-haiku-4-5 ChatAnthropic qwen3:4b ValueError: Unable to infer yes yes no no qwen3 is not a known provider, so only the first colon of ollama:qwen3:4b splits. The error comes before any request reaches a provider.
A known prefix picks the provider, a known name start picks it next, and any other string raises ValueError before a request is sent. Read from the top. Each row is one test in the order the code runs it, with a string from the capture on the path it takes.

init_chat_model with no model and configurable_fields returns a _ConfigurableModel. Its model is chosen at run time from the config, under the prefix you give, such as llm_model.

Model profiles

Model profile: a dict on model.profile with the limits and features of one model, such as its context size and whether it supports structured output.

The provider packages read profiles from data files inside each package. This is part of the profile of gpt-4.1:

jsonpart of the profile of openai:gpt-4.1Full log
  "max_input_tokens": 1047576,
  "max_output_tokens": 32768,
…
  "tool_calling": true,
  "structured_output": true,
…
  "tool_call_streaming": true,
…
  "mid_conversation_tools": false
Cut to the size limits and four features.

The profile of ollama:qwen3:4b is null, and so is the profile of the ChatOllama model of the kit. Code that reads a profile must accept None. create_agent reads structured_output from it to choose a structured output strategy, which Part 4 explains.

Retries

Retry layer: one piece of code that calls the model again after a failure. The client inside a provider package is one layer, and ModelRetryMiddleware is another.

The models page says that a chat model retries up to 6 times by default for network errors, 429, and 5xx. The retry middleware page says that its defaults make 3 attempts in total. We counted both at the pin.

We measured the attempts on the x86 server guest, 3 runs for each cell. ChatOpenAI from langchain-openai 1.8.0 called Qwen3 4B on the /v1 path of Ollama. A relay in front of Ollama dropped each connection or held it past the timeout. Raw runs

For a refused connection, nothing listened on the port. ChatOllama called the native path of Ollama the same way. The counts were the same in all 3 runs. Figure 3.4 shows each run.

Failure at the relayClient defaultMiddleware onlyBoth layers
connection dropped on every call3 attempts, then OpenAIConnectionError3 attempts, then an AIMessage9 attempts
connection refused on every call3 attempts, then OpenAIConnectionError3 attempts, then an AIMessage9 attempts
timeout on every call3 attempts, then OpenAITimeoutError3 attempts, then an AIMessage9 attempts
the real 404 of Ollama for an unknown model1 attempt, then OpenAIModelNotFoundError1 attempt1 attempt
ChatOllama, any of the failures above1 attempt, then the error of the ollama client3 attempts, then an AIMessage, also for the 404no client layer

The client default of ChatOpenAI made 3 attempts, which is 2 retries. ChatOpenAI leaves max_retries to the openai client, whose default is 2. The same 3 attempts came from init_chat_model("openai:..."). The 6 of the documentation did not hold for this package. ChatOllama made 1 attempt, because the ollama client does not retry.

With both layers on, the layers multiply. Each of the 3 middleware attempts made 3 client attempts. The client timeout was 15 s and the relay held each request 20 s. So the 9 attempts took 142 s at the median, against 46.3 s for the 3 attempts of the client alone.

With the middleware alone, the run did not raise. It ended with an AIMessage that said Model call failed after 3 attempts with OpenAIConnectionError, because the default of on_failure is continue. A 429 or a 500 that the relay sent in place of the model gave the same counts.

Fig. 3.4model attempts at the relay for one failing model callchart
Model attempts at the relay for one failing model call A dot chart of model attempts at a relay in front of Ollama with Qwen3 4B on the x86 server guest, 3 runs for each cell. Three groups for a dropped connection, a refused port, and a timeout on every call. In each group the client default of ChatOpenAI made 3 attempts, ModelRetryMiddleware with client retries off made 3, and both layers together made 9. All three runs of every cell gave the same count. 0 1 3 7 9 model attempts at the relay the documented 6 retries, 7 attempts dropped every call refused every call timeout every call client default middleware only both layers Each dot is one run on the x86 server guest. The real 404 made 1 attempt in each.
The client default and the middleware each made 3 attempts, and the two layers together made 9, in every run of every persistent failure. Each dot is one run, and each column is one configuration. The three groups are a dropped connection, a refused port, and a timeout on every call. The real 404 made 1 attempt with ChatOpenAI and is not drawn.

A one-time drop, refusal, or timeout recovered in every configuration except ChatOllama with no middleware, which raised. The client retried in the client default and with both layers. The middleware retried when it ran alone, because client retries were off there. The recovered runs had 3 model attempts, which includes the model call after the tool step.

Choose one layer. Set max_retries=0 on the model when you use ModelRetryMiddleware, or leave the middleware out. Then set your own timeout from the attempts that layer makes.

LangChain (Python): langchain, langchain-core, langgraph, deepagents, langsmith 1.5.0 · c2f4036 · 2026-11-11 · Edition 2026.11