【Bug已解决】codex: --model flag ignored / Wrong model used despite flag --- CodeX 模型参数被忽略解决方案
1. 问题描述
CodeX CLI 的 --model 参数被忽略,使用了错误的模型:
bash
# --model 被忽略
$ codex --model o1 "task"
# 实际使用 gpt-4o,而非 o1
# 或配置覆盖命令行
$ cat .codex/config.json
{"model": "gpt-4o"}
$ codex --model o1 "task"
# 仍然使用 gpt-4o
# 或环境变量覆盖
$ export CODEX_MODEL=gpt-4o
$ codex --model o1 "task"
# 仍然使用 gpt-4o
# 或旧模型名不识别
$ codex --model gpt-4 "task"
Error: model_not_found
Model 'gpt-4' is deprecated.
这个问题在以下场景中特别常见:
- 配置文件覆盖命令行
- 环境变量覆盖
- 模型名已弃用
- 大小写错误
- 版本不支持
- 参数顺序错误
2. 原因分析
原因分类表
| 原因分类 | 具体表现 | 占比 |
|---|---|---|
| 配置覆盖 | config.json | 约 35% |
| 环境变量 | CODEX_MODEL | 约 25% |
| 模型弃用 | gpt-4 | 约 20% |
| 大小写 | O1 vs o1 | 约 10% |
| 版本不支持 | 旧版本 | 约 5% |
| 参数顺序 | --model 在后 | 约 5% |
3. 解决方案
方案一:清除配置覆盖(最推荐)
bash
# 步骤 1:检查配置
cat ~/.codex/config.json | grep model
cat .codex/config.json | grep model
# 步骤 2:删除配置中的 model
python3 -c "
import json
with open('/root/.codex/config.json') as f:
config = json.load(f)
config.pop('model', None)
with open('/root/.codex/config.json', 'w') as f:
json.dump(config, f, indent=2)
"
# 步骤 3:验证
codex --model o1 "task" --max-turns 5
# 步骤 4:或使用 --model 覆盖
codex --model o1 --debug "task" 2>&1 | grep -i "model"
方案二:清除环境变量
bash
# 步骤 1:检查环境变量
env | grep -i CODEX
# 步骤 2:清除环境变量
unset CODEX_MODEL
unset CODEX_DEFAULT_MODEL
# 步骤 3:验证
codex --model o1 "task" --max-turns 5
# 步骤 4:或永久清除
sed -i '/CODEX_MODEL/d' ~/.bashrc
source ~/.bashrc
方案三:使用正确的模型名
bash
# 步骤 1:查看可用模型
codex --list-models
# 或
codex --help | grep -A20 model
# 步骤 2:使用正确的名称
codex --model o1 "task"
codex --model o1-mini "task"
codex --model gpt-4o "task"
codex --model gpt-4o-mini "task"
# 步骤 3:避免弃用的名称
# 错误: gpt-4, gpt-3.5-turbo(弃用)
# 正确: gpt-4o, gpt-4o-mini
# 步骤 4:验证
codex --model o1 --debug "task" 2>&1 | grep -i "model"
方案四:验证模型使用
bash
# 步骤 1:使用 --debug 验证
codex --model o1 --debug "task" 2>&1 | grep -i "model\|using"
# 步骤 2:检查实际使用的模型
codex --model o1 --debug "what model are you?" --max-turns 5 2>&1
# 步骤 3:对比
codex --model gpt-4o --debug "what model are you?" --max-turns 5 2>&1
# 步骤 4:确认
codex --model o1 "hello" --max-turns 1
方案五:更新配置
bash
# 步骤 1:更新 config.json
cat > .codex/config.json << 'EOF'
{
"model": "o1"
}
EOF
# 步骤 2:验证
python3 -m json.tool .codex/config.json
# 步骤 3:测试
codex "task" --max-turns 5
# 步骤 4:验证模型
codex --debug "task" 2>&1 | grep -i "model"
方案六:更新版本
bash
# 步骤 1:检查版本
codex --version
# 步骤 2:更新
npm update -g @openai/codex
# 步骤 3:验证 --model 支持
codex --help | grep -i model
# 步骤 4:测试
codex --model o1 "hello" --max-turns 1
codex --debug "hello" 2>&1 | grep -i "model"
4. 各方案对比总结
| 方案 | 适用场景 | 推荐指数 | 难度 |
|---|---|---|---|
| 方案一:清除配置 | 配置覆盖 | ⭐⭐⭐⭐⭐ | 低 |
| 方案二:清除环境变量 | env 覆盖 | ⭐⭐⭐⭐⭐ | 低 |
| 方案三:正确模型名 | 弃用 | ⭐⭐⭐⭐⭐ | 低 |
| 方案四:验证模型 | 排查 | ⭐⭐⭐⭐⭐ | 低 |
| 方案五:更新配置 | 长期 | ⭐⭐⭐⭐⭐ | 低 |
| 方案六:更新 | 版本 | ⭐⭐⭐ | 低 |
5. 常见问题 FAQ
5.1 如何查看可用模型
bash
codex --list-models
# 或
codex --help | grep -A20 model
5.2 哪些模型可用
通常: o1, o1-mini, gpt-4o, gpt-4o-mini。
5.3 配置和环境变量哪个优先级高
环境变量 > 配置文件 > 命令行参数(某些版本)。
5.4 如何验证使用的模型
bash
codex --model o1 --debug "task" 2>&1 | grep -i "model"
5.5 gpt-4 为什么不可用
已弃用。使用 gpt-4o 代替。
5.6 如何清除环境变量
bash
unset CODEX_MODEL
5.7 大小写重要吗
是的。使用小写 o1,不是 O1。
5.8 配置中的 model 字段
在 .codex/config.json 中设置 "model": "o1"。
5.9 命令行 --model 的优先级
取决于版本。某些版本配置/环境变量覆盖命令行。
5.10 排查清单速查表
□ 1. cat config.json | grep model 检查
□ 2. env | grep CODEX 检查环境变量
□ 3. unset CODEX_MODEL 清除
□ 4. 删除 config.json 中的 model
□ 5. codex --list-models 查看可用
□ 6. 使用 o1/gpt-4o(非 gpt-4)
□ 7. 小写: o1 非 O1
□ 8. codex --debug | grep model 验证
□ 9. 更新 config.json: "model": "o1"
□ 10. npm update 更新版本
6. 总结
- 根本原因:--model 被忽略最常见原因是配置覆盖(35%)和环境变量覆盖(25%)
- 最佳实践 :清除 config.json 中的 model 字段和
CODEX_MODEL环境变量 - 正确模型名 :使用
o1、gpt-4o(小写),避免弃用的gpt-4 - 验证方法 :
codex --model o1 --debug "task" 2>&1 | grep -i model - 最佳实践建议 :在 config.json 中设置
"model": "o1",用--debug验证
故障排查流程图
flowchart TD
A[--model 被忽略] --> B[codex --debug | grep model]
B --> C{用的哪个模型?}
C -->|是配置中的| D[清除 config.json model]
C -->|是环境变量| E[unset CODEX_MODEL]
C -->|是默认| F[检查模型名]
D --> G[python3 删除 model 字段]
E --> H[sed -i 删除 ~/.bashrc]
G --> I[codex --model o1 验证]
H --> I
F --> j{名称正确?}
j -->|否| k[使用 o1/gpt-4o]
j -->|是| L[检查大小写]
k --> I
L --> M{小写?}
M -->|否| N[使用小写 o1]
M -->|是| O[更新版本]
N --> I
O --> P[npm update -g @openai/codex]
P --> I
I --> Q{模型正确?}
Q -->|是| R[✅ 问题解决]
Q -->|否| S[更新 config.json]
S --> T["model": "o1"]
T --> I
I --> U{成功?}
U -->|是| R
U -->|否| V[codex --list-models 查看]
V --> W[确认可用模型]
W --> I
R --> X[长期: config.json + --debug 验证]
X --> Y[✅ 长期方案]
