Create a chat model. Uses `init_chat_model` for standard providers, or imports a custom `BaseChatModel` subclass when the provider has a `class_path` in config. Supports `provider:model` format (e.g., `'openai:gpt-5.5'`) for explicit provider selection, or bare model names for auto
(
model_spec: str | None = None,
*,
extra_kwargs: dict[str, Any] | None = None,
profile_overrides: dict[str, Any] | None = None,
)
| 4093 | |
| 4094 | |
| 4095 | def create_model( |
| 4096 | model_spec: str | None = None, |
| 4097 | *, |
| 4098 | extra_kwargs: dict[str, Any] | None = None, |
| 4099 | profile_overrides: dict[str, Any] | None = None, |
| 4100 | ) -> ModelResult: |
| 4101 | """Create a chat model. |
| 4102 | |
| 4103 | Uses `init_chat_model` for standard providers, or imports a custom |
| 4104 | `BaseChatModel` subclass when the provider has a `class_path` in config. |
| 4105 | |
| 4106 | Supports `provider:model` format (e.g., `'openai:gpt-5.5'`) |
| 4107 | for explicit provider selection, or bare model names for auto-detection. |
| 4108 | |
| 4109 | Args: |
| 4110 | model_spec: Model specification in `provider:model` format (e.g., |
| 4111 | `'anthropic:claude-sonnet-4-5'`, `'openai:gpt-5.5'`) or just the model |
| 4112 | name for auto-detection (e.g., `'claude-sonnet-4-5'`). |
| 4113 | |
| 4114 | If not provided, uses environment-based defaults. |
| 4115 | extra_kwargs: Additional kwargs to pass to the model constructor. |
| 4116 | |
| 4117 | These take highest priority, overriding values from the config file. |
| 4118 | |
| 4119 | A `CLI_MAX_RETRIES_KEY` entry (set by the `--max-retries` flag) is |
| 4120 | treated specially: it is popped here and re-applied under the |
| 4121 | provider's resolved retry-param name with top precedence, rather than |
| 4122 | being forwarded verbatim to the constructor. |
| 4123 | profile_overrides: Extra profile fields from `--profile-override`. |
| 4124 | |
| 4125 | Merged on top of config file profile overrides (dcode wins). |
| 4126 | |
| 4127 | Returns: |
| 4128 | A `ModelResult` containing the model and its metadata. |
| 4129 | |
| 4130 | Raises: |
| 4131 | ModelConfigError: If provider cannot be determined from the model name |
| 4132 | or required provider package is not installed. |
| 4133 | MissingCredentialsError: If no credentials are configured for the |
| 4134 | resolved provider. |
| 4135 | |
| 4136 | Examples: |
| 4137 | >>> model = create_model("anthropic:claude-sonnet-4-5") |
| 4138 | >>> model = create_model("openai:gpt-5.5") |
| 4139 | >>> model = create_model("gpt-5.5") # Auto-detects openai |
| 4140 | >>> model = create_model() # Uses environment defaults |
| 4141 | """ |
| 4142 | from deepagents_code.model_config import ( |
| 4143 | IMPLICIT_AUTH_PROVIDERS, |
| 4144 | ModelConfig, |
| 4145 | ModelConfigError, |
| 4146 | ModelSpec, |
| 4147 | apply_stored_credentials, |
| 4148 | get_credential_env_var, |
| 4149 | has_provider_credentials, |
| 4150 | warn_on_split_credential_source, |
| 4151 | ) |
| 4152 |