Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/crewai/src/crewai/flow/runtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,12 @@ class StateWithId(FlowState, model_class): # type: ignore[misc, valid-type]
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]:
Expand Down
33 changes: 33 additions & 0 deletions lib/crewai/tests/test_flow_from_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,27 @@ def finish(self):
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
Expand Down Expand Up @@ -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)
Expand Down
Loading