chore: use json openapi schema instead of custom python schema

This commit is contained in:
Greyson LaLonde
2025-11-04 15:21:40 -05:00
parent 01a2e218ef
commit b221db6061
3 changed files with 20 additions and 38 deletions

View File

@@ -10,9 +10,9 @@ from pydantic import BaseModel, ValidationError
from typing_extensions import Unpack
from crewai.agents.agent_builder.utilities.base_output_converter import OutputConverter
from crewai.utilities.i18n import get_i18n
from crewai.utilities.internal_instructor import InternalInstructor
from crewai.utilities.printer import Printer
from crewai.utilities.pydantic_schema_parser import PydanticSchemaParser
if TYPE_CHECKING:
@@ -22,6 +22,7 @@ if TYPE_CHECKING:
from crewai.llms.base_llm import BaseLLM
_JSON_PATTERN: Final[re.Pattern[str]] = re.compile(r"({.*})", re.DOTALL)
_I18N = get_i18n()
class ConverterError(Exception):
@@ -335,26 +336,9 @@ def get_conversion_instructions(
Returns:
"""
instructions = "Please convert the following text into valid JSON."
if (
llm
and not isinstance(llm, str)
and hasattr(llm, "supports_function_calling")
and llm.supports_function_calling()
):
model_schema = PydanticSchemaParser(model=model).get_schema()
instructions += (
f"\n\nOutput ONLY the valid JSON and nothing else.\n\n"
f"Use this format exactly:\n```json\n{model_schema}\n```"
)
else:
model_description = generate_model_description(model)
schema_json = json.dumps(model_description["json_schema"]["schema"], indent=2)
instructions += (
f"\n\nOutput ONLY the valid JSON and nothing else.\n\n"
f"Use this format exactly:\n```json\n{schema_json}\n```"
)
return instructions
schema_dict = generate_model_description(model)
schema = json.dumps(schema_dict, indent=2)
return _I18N.slice("formatted_task_instructions").format(output_format=schema)
class CreateConverterKwargs(TypedDict, total=False):

View File

@@ -13,6 +13,8 @@ load_result = load_dotenv(override=True)
@pytest.fixture(autouse=True)
def setup_test_environment():
"""Set up test environment with a temporary directory for SQLite storage."""
import shutil
with tempfile.TemporaryDirectory() as temp_dir:
# Create the directory with proper permissions
storage_dir = Path(temp_dir) / "crewai_test_storage"
@@ -40,7 +42,9 @@ def setup_test_environment():
yield
os.environ.pop("CREWAI_TESTING", None)
# Cleanup is handled automatically when tempfile context exits
# Force cleanup of storage directory before tempfile context exits
if storage_dir.exists():
shutil.rmtree(storage_dir, ignore_errors=True)
def pytest_configure(config):

View File

@@ -227,28 +227,22 @@ def test_get_conversion_instructions_gpt() -> None:
with patch.object(LLM, "supports_function_calling") as supports_function_calling:
supports_function_calling.return_value = True
instructions = get_conversion_instructions(SimpleModel, llm)
model_schema = PydanticSchemaParser(model=SimpleModel).get_schema()
expected_instructions = (
"Please convert the following text into valid JSON.\n\n"
"Output ONLY the valid JSON and nothing else.\n\n"
"Use this format exactly:\n```json\n"
f"{model_schema}\n```"
)
assert instructions == expected_instructions
assert "Ensure your final answer strictly adheres to the following OpenAPI schema:" in instructions
assert "Do not include the OpenAPI schema in the final output" in instructions
assert "json_schema" in instructions
assert '"name": "SimpleModel"' in instructions
assert '"strict": true' in instructions
def test_get_conversion_instructions_non_gpt() -> None:
llm = LLM(model="ollama/llama3.1", base_url="http://localhost:11434")
with patch.object(LLM, "supports_function_calling", return_value=False):
instructions = get_conversion_instructions(SimpleModel, llm)
# Check that the JSON schema is properly formatted
assert "Please convert the following text into valid JSON" in instructions
assert "Output ONLY the valid JSON and nothing else" in instructions
assert "Use this format exactly" in instructions
assert "```json" in instructions
assert '"type": "object"' in instructions
assert '"properties"' in instructions
assert "'type': 'json_schema'" not in instructions
assert "Ensure your final answer strictly adheres to the following OpenAPI schema:" in instructions
assert "Do not include the OpenAPI schema in the final output" in instructions
assert "json_schema" in instructions
assert '"name": "SimpleModel"' in instructions
assert '"strict": true' in instructions
# Tests for is_gpt