# 运行时错误：未返回响应。

- **ID:** `python/fastapi-dependency-yield-cleanup`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在 FastAPI 中，带有 yield 的依赖必须作为生成器使用；如果忘记 yield 或 return，路由会失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   def get_db():
    db = Session()
    try:
        yield db
    finally:
        db.close()

@app.get('/')
def read(db = Depends(get_db)):
    return {'db': db}
   ```
2. **** (85% 成功率)
   ```
   If you need to return, use a regular function without yield.
   ```

## 无效尝试

- **** — FastAPI expects a generator for cleanup; return breaks it. (90% 失败率)
- **** — Dependency not invoked; no response generated. (70% 失败率)
