Python: Fixed declarative samples (#4051)

* Updated declarative kind mapping

* Fixed required property handling

* Updated inline yaml sample

* Fixed remaining declarative samples

* Added lazy initialization for PowerFx engine

* Small fix
This commit is contained in:
Dmytro Struk
2026-02-18 15:08:31 -08:00
committed by GitHub
Unverified
parent 1b87a07377
commit 57da1bcfeb
9 changed files with 173 additions and 16 deletions
@@ -556,6 +556,58 @@ instructions: You are a helpful assistant.
with pytest.raises(DeclarativeLoaderError, match="ChatClient must be provided"):
factory.create_agent_from_dict(agent_def)
def test_create_agent_from_dict_output_schema_in_default_options(self):
"""Test that outputSchema is passed as response_format in Agent.default_options."""
from unittest.mock import MagicMock
from pydantic import BaseModel
from agent_framework_declarative import AgentFactory
agent_def = {
"kind": "Prompt",
"name": "TestAgent",
"instructions": "You are helpful.",
"outputSchema": {
"properties": {
"answer": {"type": "string", "required": True, "description": "The answer."},
},
},
}
mock_client = MagicMock()
factory = AgentFactory(client=mock_client)
agent = factory.create_agent_from_dict(agent_def)
assert "response_format" in agent.default_options
assert isinstance(agent.default_options["response_format"], type)
assert issubclass(agent.default_options["response_format"], BaseModel)
def test_create_agent_from_dict_chat_options_in_default_options(self):
"""Test that chat options (temperature, top_p) are in Agent.default_options."""
from unittest.mock import MagicMock
from agent_framework_declarative import AgentFactory
agent_def = {
"kind": "Prompt",
"name": "TestAgent",
"instructions": "You are helpful.",
"model": {
"options": {
"temperature": 0.7,
"topP": 0.9,
},
},
}
mock_client = MagicMock()
factory = AgentFactory(client=mock_client)
agent = factory.create_agent_from_dict(agent_def)
assert agent.default_options.get("temperature") == 0.7
assert agent.default_options.get("top_p") == 0.9
class TestAgentFactorySafeMode:
"""Tests for AgentFactory safe_mode parameter."""
@@ -103,6 +103,50 @@ class TestProperty:
assert prop.description == "A test property"
assert prop.required is True
def test_property_from_dict_type_maps_to_kind(self):
"""Test that 'type' field in YAML is mapped to 'kind' internally."""
data = {
"name": "test_prop",
"type": "string",
"description": "A test property",
"required": True,
}
prop = Property.from_dict(data)
assert prop.name == "test_prop"
assert prop.kind == "string"
def test_property_from_dict_kind_takes_precedence_over_type(self):
"""Test that 'kind' takes precedence when both 'type' and 'kind' are present."""
data = {
"name": "test_prop",
"type": "integer",
"kind": "string",
}
prop = Property.from_dict(data)
assert prop.kind == "string"
def test_property_from_dict_type_dispatches_to_array(self):
"""Test that 'type: array' correctly dispatches to ArrayProperty."""
data = {
"name": "test_array",
"type": "array",
"items": {"type": "string"},
}
prop = Property.from_dict(data)
assert isinstance(prop, ArrayProperty)
assert prop.kind == "array"
def test_property_from_dict_type_dispatches_to_object(self):
"""Test that 'type: object' correctly dispatches to ObjectProperty."""
data = {
"name": "test_object",
"type": "object",
"properties": {"field": {"type": "string"}},
}
prop = Property.from_dict(data)
assert isinstance(prop, ObjectProperty)
assert prop.kind == "object"
class TestArrayProperty:
"""Tests for ArrayProperty class."""
@@ -230,6 +274,29 @@ class TestPropertySchema:
assert age_prop.kind == "integer"
assert age_prop.required is True
def test_property_schema_with_type_field_produces_correct_json_schema(self):
"""Test that PropertySchema with 'type' fields (YAML spec format) produces valid JSON schema."""
data = {
"properties": {
"language": {"type": "string", "required": True, "description": "The language."},
"answer": {"type": "string", "required": False, "description": "The answer."},
},
}
schema = PropertySchema.from_dict(data)
assert len(schema.properties) == 2
lang_prop = next(p for p in schema.properties if p.name == "language")
assert lang_prop.kind == "string"
json_schema = schema.to_json_schema()
assert json_schema["type"] == "object"
assert json_schema["properties"]["language"]["type"] == "string"
assert json_schema["properties"]["answer"]["type"] == "string"
# required is a top-level array, not a per-property boolean
assert json_schema["required"] == ["language"]
assert "required" not in json_schema["properties"]["language"]
assert "required" not in json_schema["properties"]["answer"]
class TestConnection:
"""Tests for Connection base class."""