Async context-manager tools leak entered resources when callable validation fails

Abierto
#1,977 0 comentarios 0 reacciones 0 asignados Ver en GitHub

@sylvesterkaczmarek ya está trabajando en esto.

Desde el 5/10/2026.

  • #1978 de @sylvesterkaczmarek — abierto
  • #2001 de @iosayin — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
python
Área
api, backend

Línea de trabajo

Start at beta_async_tool and compare its async context-manager setup failure path with the synchronous branch, which the issue says already cleans up construction failures. Run the two async regression cases described in the issue, including the case where __aexit__ suppresses the validation exception; done means awaited cleanup receives the active exception and the setup failure is re-raised unless suppressed. A linked pull request is already open.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

On main at 18f25547f20cf5f01da69ac611e700e3bc9ebf21, beta_async_tool enters an async context manager before validating its yielded callable. If pydantic.validate_call(inner) raises, the entered manager is neither exited immediately nor registered for subsequent tool cleanup.

A reproducible case is an @asynccontextmanager that acquires a resource and yields an async callable with an unsupported custom parameter type. Wrap it with beta_async_tool(name="invalid", input_schema={"type": "object"}) and invoke .call(...). Pydantic raises PydanticSchemaGenerationError, but the context manager's finally block has not run when that call finishes.

Expected: pass the active exception to __aexit__, await cleanup, and re-raise the setup failure. The synchronous context-manager branch already does this for construction failures.

Two async regression cases reproduce the missing cleanup, including a manager that tries to suppress the validation exception. A synchronous counterpart is a passing control. The async cleanup itself awaits, so checking only that an exit coroutine was created would not satisfy the regression.

Reproduced on macOS with Python 3.14.7 and Pydantic v2. No live API calls are needed.

Lenguaje dominante
Python
Estrellas
4.1k
Forks
915
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de anthropics/anthropic-sdk-python

Todos los issues de anthropics/anthropic-sdk-python

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.