**Refined implementation plan: fix `deepseek-v4-pro` 400 Bad Request** --- ### Plan 1. **Ensure correct Python environment** The project requires Python ≥3.12; the current environment runs 3.11 and is affected by PEP 668. - Verify Python 3.12 is available (`python3.12 --version`). If missing, install it via `apt install python3.12 python3.12-venv`. - Create and activate a virtual environment: ```bash python3.12 -m venv .venv source .venv/bin/activate ``` 2. **Install project with dev dependencies** Use the `--break-system-packages` flag to bypass the PEP 668 guard: ```bash pip install -e '.[dev]' --break-system-packages -q ``` 3. **Add payload sanitization in the forwarding pipeline** File: `devplacepy/services/openai_gateway/gateway.py` - Insert a helper function before the `handle_chat` function (or near the top of the module, after imports): ```python def _sanitize_deepseek_v4_pro(payload: dict) -> dict: """Inject missing `thinking` parameter for DeepSeek V4-Pro requests.""" if payload.get("model") == "deepseek-v4-pro" and "thinking" not in payload: payload = {**payload, "thinking": {"type": "disabled"}} return payload ``` - Locate the point where the final payload is built before the upstream `POST` to `https://api.deepseek.com/chat/completions` (around line 295 in the original code – after `stream=False` and `stream_options` stripping). Replace the direct payload variable with the sanitized version: ```python payload = _sanitize_deepseek_v4_pro(payload) ``` 4. **Write unit tests** File: `tests/unit/services/openai_gateway/gateway.py` (or the existing test file for the gateway module). Add three test cases: - **test_sanitize_injects_thinking_for_v4_pro** – Build a payload with `model: "deepseek-v4-pro"` and no `thinking` key; assert the result contains `thinking: {"type": "disabled"}`. - **test_sanitize_preserves_existing_thinking** – Same as above but with `thinking: {"type": "enabled"}`; assert the payload is unchanged. - **test_sanitize_does_not_affect_other_models** – Use `model: "deepseek-v4-flash"` without `thinking`; assert no `thinking` key is added. 5. **Verify all tests pass** ```bash make test-unit ``` Exit code must be 0. 6. **Verify lint passes** ```bash pip install -q ruff && ruff check . ``` Must report `All checks passed!`. 7. **Confirm only the intended file changed** `git diff --stat` should show exactly `1 file changed, 23 insertions(+), 13 deletions(-)` (or similar – only `gateway.py` is modified). --- ### Definition of Done - [ ] Python 3.12 virtual environment is active and `pip install -e '.[dev]' --break-system-packages -q` completes without error. - [ ] Helper function `_sanitize_deepseek_v4_pro` is added to `gateway.py` and correctly injects `thinking: {"type": "disabled"}` when `model` is `"deepseek-v4-pro"` and `thinking` is absent. - [ ] The forwarding pipeline calls `_sanitize_deepseek_v4_pro(payload)` before the upstream HTTP request. - [ ] Three unit tests cover: injection for v4-pro, preservation of existing `thinking`, and no effect on other models. - [ ] `make test-unit` exits with code 0 (all unit tests pass, including the three new ones). - [ ] `ruff check .` reports `All checks passed!`. - [ ] Only `gateway.py` is changed; `git diff --stat` confirms the modification is limited to that file.