Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

Fix JSON schema flow state kickoff inputs (#6325)

Vinicius Brasil156b3500b4ef
diff --git a/lib/crewai/src/crewai/flow/runtime/__init__.py b/lib/crewai/src/crewai/flow/runtime/__init__.py
index 4b07f3533..24f863c4e 100644
--- a/lib/crewai/src/crewai/flow/runtime/__init__.py
+++ b/lib/crewai/src/crewai/flow/runtime/__init__.py
@@ -227,7 +227,12 @@ def _build_definition_state_model(
pass
model_class = StateWithId
- return model_class(**kwargs)
+ try:
+ return model_class(**kwargs)
+ except ValidationError as e:
+ if any(error.get("type") != "missing" for error in e.errors()):
+ raise
+ return model_class.model_construct(**kwargs)
def _iter_condition_events(condition: FlowDefinitionCondition) -> Iterator[str]:
diff --git a/lib/crewai/tests/test_flow_from_definition.py b/lib/crewai/tests/test_flow_from_definition.py
index 909f56459..16160f3cf 100644
--- a/lib/crewai/tests/test_flow_from_definition.py
+++ b/lib/crewai/tests/test_flow_from_definition.py
@@ -357,6 +357,27 @@ methods:
listen: begin
"""
+JSON_SCHEMA_REQUIRED_INPUT_STATE_YAML = """
+schema: crewai.flow/v1
+name: JsonSchemaRequiredInputStateFlow
+state:
+ type: json_schema
+ json_schema:
+ title: LeadState
+ type: object
+ required:
+ - lead_name
+ properties:
+ lead_name:
+ type: string
+methods:
+ begin:
+ start: true
+ do:
+ call: expression
+ expr: state.lead_name
+"""
+
PYDANTIC_REF_WITH_SCHEMA_FALLBACK_YAML = f"""
schema: crewai.flow/v1
name: SchemaFallbackFlow
@@ -2298,6 +2319,18 @@ def test_json_schema_state_validates_inputs():
flow.kickoff(inputs={"count": "not-a-number"})
+def test_json_schema_state_required_fields_can_come_from_kickoff_inputs():
+ flow = Flow.from_definition(
+ FlowDefinition.from_declaration(contents=JSON_SCHEMA_REQUIRED_INPUT_STATE_YAML)
+ )
+
+ result = flow.kickoff(inputs={"lead_name": "Ada Lovelace"})
+
+ assert result == "Ada Lovelace"
+ assert flow.state.lead_name == "Ada Lovelace"
+ assert flow.state.id
+
+
def test_pydantic_state_falls_back_to_json_schema_when_ref_unimportable():
flow = Flow.from_definition(
FlowDefinition.from_declaration(contents=PYDANTIC_REF_WITH_SCHEMA_FALLBACK_YAML)