Repository navigation
Description
Description
Converter._coerce_response_to_pydantic returns any BaseModel unchanged, even when it is not an instance of the configured model. A custom LLM returning another structured model can bypass required fields, coercion and defaults. The shared helper affects to_pydantic/ato_pydantic and the text-only to_json/ato_json paths.
Steps to Reproduce / Code
from unittest.mock import Mock
from pydantic import BaseModel
from crewai.utilities.converter import Converter
class Report(BaseModel):
count: int
status: str = "ready"
class ProviderReport(BaseModel):
count: str
llm = Mock()
llm.supports_function_calling.return_value = False
llm.call.return_value = ProviderReport(count="invalid")
result = Converter(llm=llm, text="items", model=Report,
instructions="Return a report.", max_attempts=2).to_pydantic()
print(type(result).__name__, result.model_dump())Actual: ProviderReport {'count': 'invalid'}, with one call and no validation failure.
Expected behavior
Validate a different BaseModel against Report. For count="3", return Report(count=3, status="ready"). For count="invalid", use the existing bounded retry/error behavior. An instance already of the requested model should retain the existing fast path.
Environment
macOS, Python 3.12.14, main 56c26a7. Source imported from the checkout (not the installed wheel). Independent uv venv; pytest 9.0.3, Pydantic 2.12.5. CrewAI Tools is not involved.
Evidence
Eight new cases fail on unmodified main; one matching-model identity control passes. The eight cases cover valid/invalid foreign-model responses across sync/async Pydantic and text-only JSON conversion. A minimal fix passes all nine. No provider credentials or model request is needed.
Possible Solution
Keep the fast path only for an instance of self.model. Serialize other BaseModel responses and feed them through the existing target-model JSON validation and retry path.
Additional context
Found, implemented and tested with Codex assistance under the account owner's authorization. Please apply llm-generated: this integration's attempt to label an upstream contribution returned HTTP 403 (Resource not accessible by integration). Searches of all 799 open PR titles and all-state converter PR searches found no matching foreign-model validation fix; agent-reference fallback and partial-JSON extraction PRs address other behavior.
Activity