vellum-ai 0.10.8__py3-none-any.whl → 0.11.0__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- vellum/client/core/client_wrapper.py +1 -1
- vellum/client/types/logical_operator.py +2 -0
- vellum/evaluations/resources.py +7 -12
- vellum/evaluations/utils/env.py +1 -3
- vellum/evaluations/utils/paginator.py +0 -1
- vellum/evaluations/utils/typing.py +1 -1
- vellum/evaluations/utils/uuid.py +1 -1
- vellum/plugins/vellum_mypy.py +3 -1
- vellum/workflows/descriptors/utils.py +27 -0
- vellum/workflows/events/__init__.py +0 -2
- vellum/workflows/events/node.py +7 -6
- vellum/workflows/events/tests/test_event.py +2 -2
- vellum/workflows/events/types.py +35 -30
- vellum/workflows/events/workflow.py +33 -8
- vellum/workflows/nodes/bases/base.py +49 -26
- vellum/workflows/nodes/bases/tests/test_base_node.py +0 -1
- vellum/workflows/nodes/core/templating_node/node.py +1 -0
- vellum/workflows/nodes/core/try_node/node.py +22 -4
- vellum/workflows/nodes/core/try_node/tests/test_node.py +16 -3
- vellum/workflows/nodes/displayable/bases/api_node/node.py +1 -1
- vellum/workflows/nodes/displayable/bases/base_prompt_node/node.py +0 -1
- vellum/workflows/nodes/displayable/bases/inline_prompt_node/node.py +0 -1
- vellum/workflows/nodes/displayable/bases/prompt_deployment_node.py +2 -1
- vellum/workflows/nodes/displayable/bases/search_node.py +0 -1
- vellum/workflows/nodes/displayable/code_execution_node/tests/test_code_execution_node.py +0 -1
- vellum/workflows/nodes/displayable/code_execution_node/utils.py +3 -2
- vellum/workflows/nodes/displayable/conditional_node/node.py +1 -1
- vellum/workflows/nodes/displayable/guardrail_node/node.py +0 -1
- vellum/workflows/nodes/displayable/inline_prompt_node/node.py +1 -0
- vellum/workflows/nodes/displayable/prompt_deployment_node/node.py +3 -1
- vellum/workflows/nodes/displayable/search_node/node.py +1 -0
- vellum/workflows/nodes/displayable/subworkflow_deployment_node/node.py +3 -2
- vellum/workflows/nodes/displayable/tests/test_inline_text_prompt_node.py +10 -7
- vellum/workflows/nodes/displayable/tests/test_search_node_wth_text_output.py +0 -1
- vellum/workflows/outputs/base.py +2 -4
- vellum/workflows/ports/node_ports.py +1 -1
- vellum/workflows/runner/runner.py +185 -157
- vellum/workflows/state/base.py +55 -23
- vellum/workflows/state/context.py +26 -3
- vellum/workflows/types/core.py +1 -0
- vellum/workflows/types/tests/test_utils.py +1 -0
- vellum/workflows/types/utils.py +0 -1
- vellum/workflows/utils/functions.py +74 -0
- vellum/workflows/utils/tests/test_functions.py +171 -0
- vellum/workflows/utils/tests/test_vellum_variables.py +0 -1
- vellum/workflows/utils/vellum_variables.py +2 -2
- vellum/workflows/workflows/base.py +84 -10
- vellum/workflows/workflows/event_filters.py +53 -0
- {vellum_ai-0.10.8.dist-info → vellum_ai-0.11.0.dist-info}/METADATA +1 -1
- {vellum_ai-0.10.8.dist-info → vellum_ai-0.11.0.dist-info}/RECORD +101 -93
- vellum_cli/__init__.py +147 -13
- vellum_cli/config.py +0 -1
- vellum_cli/image_push.py +1 -1
- vellum_cli/pull.py +29 -19
- vellum_cli/push.py +9 -10
- vellum_cli/tests/__init__.py +0 -0
- vellum_cli/tests/conftest.py +40 -0
- vellum_cli/tests/test_main.py +11 -0
- vellum_cli/tests/test_pull.py +125 -71
- vellum_cli/tests/test_push.py +173 -0
- vellum_ee/workflows/display/nodes/base_node_display.py +3 -2
- vellum_ee/workflows/display/nodes/base_node_vellum_display.py +2 -2
- vellum_ee/workflows/display/nodes/get_node_display_class.py +1 -1
- vellum_ee/workflows/display/nodes/tests/test_base_node_display.py +1 -1
- vellum_ee/workflows/display/nodes/vellum/__init__.py +5 -3
- vellum_ee/workflows/display/nodes/vellum/api_node.py +4 -7
- vellum_ee/workflows/display/nodes/vellum/conditional_node.py +39 -22
- vellum_ee/workflows/display/nodes/vellum/error_node.py +49 -0
- vellum_ee/workflows/display/nodes/vellum/final_output_node.py +0 -2
- vellum_ee/workflows/display/nodes/vellum/guardrail_node.py +1 -1
- vellum_ee/workflows/display/nodes/vellum/inline_prompt_node.py +1 -1
- vellum_ee/workflows/display/nodes/vellum/inline_subworkflow_node.py +4 -2
- vellum_ee/workflows/display/nodes/vellum/map_node.py +11 -5
- vellum_ee/workflows/display/nodes/vellum/merge_node.py +2 -2
- vellum_ee/workflows/display/nodes/vellum/note_node.py +1 -3
- vellum_ee/workflows/display/nodes/vellum/prompt_deployment_node.py +1 -1
- vellum_ee/workflows/display/nodes/vellum/search_node.py +1 -1
- vellum_ee/workflows/display/nodes/vellum/subworkflow_deployment_node.py +1 -1
- vellum_ee/workflows/display/nodes/vellum/templating_node.py +1 -1
- vellum_ee/workflows/display/nodes/vellum/tests/test_utils.py +5 -5
- vellum_ee/workflows/display/nodes/vellum/utils.py +4 -4
- vellum_ee/workflows/display/tests/test_vellum_workflow_display.py +45 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_api_node_serialization.py +13 -24
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_conditional_node_serialization.py +13 -39
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_error_node_serialization.py +203 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_guardrail_node_serialization.py +2 -2
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_inline_subworkflow_serialization.py +62 -58
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_map_node_serialization.py +25 -4
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_merge_node_serialization.py +2 -1
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_prompt_deployment_serialization.py +2 -2
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_subworkflow_deployment_serialization.py +2 -2
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_terminal_node_serialization.py +1 -1
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_try_node_serialization.py +2 -1
- vellum_ee/workflows/display/tests/workflow_serialization/test_complex_terminal_node_serialization.py +2 -2
- vellum_ee/workflows/display/types.py +4 -4
- vellum_ee/workflows/display/utils/vellum.py +2 -6
- vellum_ee/workflows/display/workflows/get_vellum_workflow_display_class.py +4 -1
- vellum_ee/workflows/display/workflows/vellum_workflow_display.py +6 -2
- vellum/workflows/events/utils.py +0 -5
- vellum/workflows/runner/types.py +0 -16
- {vellum_ai-0.10.8.dist-info → vellum_ai-0.11.0.dist-info}/LICENSE +0 -0
- {vellum_ai-0.10.8.dist-info → vellum_ai-0.11.0.dist-info}/WHEEL +0 -0
- {vellum_ai-0.10.8.dist-info → vellum_ai-0.11.0.dist-info}/entry_points.txt +0 -0
vellum/workflows/state/base.py
CHANGED
@@ -5,15 +5,15 @@ from datetime import datetime
|
|
5
5
|
from queue import Queue
|
6
6
|
from threading import Lock
|
7
7
|
from uuid import UUID, uuid4
|
8
|
-
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterator, Optional, Sequence, Set, Tuple, Type, cast
|
8
|
+
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterator, List, Optional, Sequence, Set, Tuple, Type, cast
|
9
9
|
from typing_extensions import dataclass_transform
|
10
10
|
|
11
11
|
from pydantic import GetCoreSchemaHandler, field_serializer
|
12
12
|
from pydantic_core import core_schema
|
13
13
|
|
14
14
|
from vellum.core.pydantic_utilities import UniversalBaseModel
|
15
|
-
|
16
15
|
from vellum.workflows.constants import UNDEF
|
16
|
+
from vellum.workflows.edges.edge import Edge
|
17
17
|
from vellum.workflows.inputs.base import BaseInputs
|
18
18
|
from vellum.workflows.references import ExternalInputReference, OutputReference, StateValueReference
|
19
19
|
from vellum.workflows.types.generics import StateType
|
@@ -71,58 +71,92 @@ def _make_snapshottable(value: Any, snapshot_callback: Callable[[], None]) -> An
|
|
71
71
|
|
72
72
|
|
73
73
|
class NodeExecutionCache:
|
74
|
-
|
74
|
+
_node_executions_fulfilled: Dict[Type["BaseNode"], Stack[UUID]]
|
75
75
|
_node_executions_initiated: Dict[Type["BaseNode"], Set[UUID]]
|
76
|
-
|
76
|
+
_node_executions_queued: Dict[Type["BaseNode"], List[UUID]]
|
77
|
+
_dependencies_invoked: Dict[UUID, Set[Type["BaseNode"]]]
|
77
78
|
|
78
79
|
def __init__(
|
79
80
|
self,
|
80
81
|
dependencies_invoked: Optional[Dict[str, Sequence[str]]] = None,
|
81
|
-
|
82
|
+
node_executions_fulfilled: Optional[Dict[str, Sequence[str]]] = None,
|
82
83
|
node_executions_initiated: Optional[Dict[str, Sequence[str]]] = None,
|
84
|
+
node_executions_queued: Optional[Dict[str, Sequence[str]]] = None,
|
83
85
|
) -> None:
|
84
86
|
self._dependencies_invoked = defaultdict(set)
|
85
|
-
self.
|
87
|
+
self._node_executions_fulfilled = defaultdict(Stack[UUID])
|
86
88
|
self._node_executions_initiated = defaultdict(set)
|
89
|
+
self._node_executions_queued = defaultdict(list)
|
87
90
|
|
88
|
-
for
|
89
|
-
self._dependencies_invoked[
|
91
|
+
for execution_id, dependencies in (dependencies_invoked or {}).items():
|
92
|
+
self._dependencies_invoked[UUID(execution_id)] = {get_class_by_qualname(dep) for dep in dependencies}
|
90
93
|
|
91
|
-
for node, execution_ids in (
|
94
|
+
for node, execution_ids in (node_executions_fulfilled or {}).items():
|
92
95
|
node_class = get_class_by_qualname(node)
|
93
|
-
self.
|
96
|
+
self._node_executions_fulfilled[node_class].extend(UUID(execution_id) for execution_id in execution_ids)
|
94
97
|
|
95
98
|
for node, execution_ids in (node_executions_initiated or {}).items():
|
96
99
|
node_class = get_class_by_qualname(node)
|
97
100
|
self._node_executions_initiated[node_class].update({UUID(execution_id) for execution_id in execution_ids})
|
98
101
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
+
for node, execution_ids in (node_executions_queued or {}).items():
|
103
|
+
node_class = get_class_by_qualname(node)
|
104
|
+
self._node_executions_queued[node_class].extend(UUID(execution_id) for execution_id in execution_ids)
|
102
105
|
|
103
|
-
def
|
104
|
-
|
106
|
+
def _invoke_dependency(
|
107
|
+
self,
|
108
|
+
execution_id: UUID,
|
109
|
+
node: Type["BaseNode"],
|
110
|
+
dependency: Type["BaseNode"],
|
111
|
+
dependencies: Set["Type[BaseNode]"],
|
112
|
+
) -> None:
|
113
|
+
self._dependencies_invoked[execution_id].add(dependency)
|
114
|
+
if all(dep in self._dependencies_invoked[execution_id] for dep in dependencies):
|
115
|
+
self._node_executions_queued[node].remove(execution_id)
|
116
|
+
|
117
|
+
def queue_node_execution(
|
118
|
+
self, node: Type["BaseNode"], dependencies: Set["Type[BaseNode]"], invoked_by: Optional[Edge] = None
|
119
|
+
) -> UUID:
|
120
|
+
execution_id = uuid4()
|
121
|
+
if not invoked_by:
|
122
|
+
return execution_id
|
123
|
+
|
124
|
+
source_node = invoked_by.from_port.node_class
|
125
|
+
for queued_node_execution_id in self._node_executions_queued[node]:
|
126
|
+
if source_node not in self._dependencies_invoked[queued_node_execution_id]:
|
127
|
+
self._invoke_dependency(queued_node_execution_id, node, source_node, dependencies)
|
128
|
+
return queued_node_execution_id
|
129
|
+
|
130
|
+
self._node_executions_queued[node].append(execution_id)
|
131
|
+
self._invoke_dependency(execution_id, node, source_node, dependencies)
|
132
|
+
return execution_id
|
133
|
+
|
134
|
+
def is_node_execution_initiated(self, node: Type["BaseNode"], execution_id: UUID) -> bool:
|
135
|
+
return execution_id in self._node_executions_initiated[node]
|
105
136
|
|
106
137
|
def initiate_node_execution(self, node: Type["BaseNode"], execution_id: UUID) -> None:
|
107
138
|
self._node_executions_initiated[node].add(execution_id)
|
108
139
|
|
109
140
|
def fulfill_node_execution(self, node: Type["BaseNode"], execution_id: UUID) -> None:
|
110
|
-
self.
|
111
|
-
self._node_execution_ids[node].push(execution_id)
|
141
|
+
self._node_executions_fulfilled[node].push(execution_id)
|
112
142
|
|
113
143
|
def get_execution_count(self, node: Type["BaseNode"]) -> int:
|
114
|
-
return self.
|
144
|
+
return self._node_executions_fulfilled[node].size()
|
115
145
|
|
116
146
|
def dump(self) -> Dict[str, Any]:
|
117
147
|
return {
|
118
148
|
"dependencies_invoked": {
|
119
|
-
|
149
|
+
str(execution_id): [str(dep) for dep in dependencies]
|
150
|
+
for execution_id, dependencies in self._dependencies_invoked.items()
|
120
151
|
},
|
121
152
|
"node_executions_initiated": {
|
122
153
|
str(node): list(execution_ids) for node, execution_ids in self._node_executions_initiated.items()
|
123
154
|
},
|
124
|
-
"
|
125
|
-
str(node): execution_ids.dump() for node, execution_ids in self.
|
155
|
+
"node_executions_fulfilled": {
|
156
|
+
str(node): execution_ids.dump() for node, execution_ids in self._node_executions_fulfilled.items()
|
157
|
+
},
|
158
|
+
"node_executions_queued": {
|
159
|
+
str(node): execution_ids for node, execution_ids in self._node_executions_queued.items()
|
126
160
|
},
|
127
161
|
}
|
128
162
|
|
@@ -158,7 +192,6 @@ class StateMeta(UniversalBaseModel):
|
|
158
192
|
node_outputs: Dict[OutputReference, Any] = field(default_factory=dict)
|
159
193
|
node_execution_cache: NodeExecutionCache = field(default_factory=NodeExecutionCache)
|
160
194
|
parent: Optional["BaseState"] = None
|
161
|
-
is_terminated: Optional[bool] = None
|
162
195
|
__snapshot_callback__: Optional[Callable[[], None]] = field(init=False, default=None)
|
163
196
|
|
164
197
|
def model_post_init(self, context: Any) -> None:
|
@@ -252,7 +285,6 @@ class BaseState(metaclass=_BaseStateMeta):
|
|
252
285
|
{values}
|
253
286
|
meta:
|
254
287
|
id={self.meta.id}
|
255
|
-
is_terminated={self.meta.is_terminated}
|
256
288
|
updated_ts={self.meta.updated_ts}
|
257
289
|
node_outputs:{' Empty' if not node_outputs else ''}
|
258
290
|
{node_outputs}
|
@@ -1,14 +1,24 @@
|
|
1
1
|
from functools import cached_property
|
2
|
-
from
|
2
|
+
from queue import Queue
|
3
|
+
from typing import TYPE_CHECKING, Optional
|
3
4
|
|
4
5
|
from vellum import Vellum
|
5
|
-
|
6
|
+
from vellum.workflows.events.types import ParentContext
|
6
7
|
from vellum.workflows.vellum_client import create_vellum_client
|
7
8
|
|
9
|
+
if TYPE_CHECKING:
|
10
|
+
from vellum.workflows.events.workflow import WorkflowEvent
|
11
|
+
|
8
12
|
|
9
13
|
class WorkflowContext:
|
10
|
-
def __init__(
|
14
|
+
def __init__(
|
15
|
+
self,
|
16
|
+
_vellum_client: Optional[Vellum] = None,
|
17
|
+
_parent_context: Optional[ParentContext] = None,
|
18
|
+
):
|
11
19
|
self._vellum_client = _vellum_client
|
20
|
+
self._parent_context = _parent_context
|
21
|
+
self._event_queue: Optional[Queue["WorkflowEvent"]] = None
|
12
22
|
|
13
23
|
@cached_property
|
14
24
|
def vellum_client(self) -> Vellum:
|
@@ -16,3 +26,16 @@ class WorkflowContext:
|
|
16
26
|
return self._vellum_client
|
17
27
|
|
18
28
|
return create_vellum_client()
|
29
|
+
|
30
|
+
@cached_property
|
31
|
+
def parent_context(self) -> Optional[ParentContext]:
|
32
|
+
if self._parent_context:
|
33
|
+
return self._parent_context
|
34
|
+
return None
|
35
|
+
|
36
|
+
def _emit_subworkflow_event(self, event: "WorkflowEvent") -> None:
|
37
|
+
if self._event_queue:
|
38
|
+
self._event_queue.put(event)
|
39
|
+
|
40
|
+
def _register_event_queue(self, event_queue: Queue["WorkflowEvent"]) -> None:
|
41
|
+
self._event_queue = event_queue
|
vellum/workflows/types/core.py
CHANGED
vellum/workflows/types/utils.py
CHANGED
@@ -0,0 +1,74 @@
|
|
1
|
+
import dataclasses
|
2
|
+
import inspect
|
3
|
+
from typing import Any, Callable, Union, get_args, get_origin
|
4
|
+
|
5
|
+
from vellum.client.types.function_definition import FunctionDefinition
|
6
|
+
|
7
|
+
type_map = {
|
8
|
+
str: "string",
|
9
|
+
int: "integer",
|
10
|
+
float: "number",
|
11
|
+
bool: "boolean",
|
12
|
+
list: "array",
|
13
|
+
dict: "object",
|
14
|
+
None: "null",
|
15
|
+
type(None): "null",
|
16
|
+
}
|
17
|
+
|
18
|
+
|
19
|
+
def _compile_annotation(annotation: Any, defs: dict[str, Any]) -> dict:
|
20
|
+
if get_origin(annotation) is Union:
|
21
|
+
return {"anyOf": [_compile_annotation(a, defs) for a in get_args(annotation)]}
|
22
|
+
|
23
|
+
if get_origin(annotation) is dict:
|
24
|
+
_, value_type = get_args(annotation)
|
25
|
+
return {"type": "object", "additionalProperties": _compile_annotation(value_type, defs)}
|
26
|
+
|
27
|
+
if get_origin(annotation) is list:
|
28
|
+
item_type = get_args(annotation)[0]
|
29
|
+
return {"type": "array", "items": _compile_annotation(item_type, defs)}
|
30
|
+
|
31
|
+
if dataclasses.is_dataclass(annotation):
|
32
|
+
if annotation.__name__ not in defs:
|
33
|
+
properties = {}
|
34
|
+
required = []
|
35
|
+
for field in dataclasses.fields(annotation):
|
36
|
+
properties[field.name] = _compile_annotation(field.type, defs)
|
37
|
+
if field.default is dataclasses.MISSING:
|
38
|
+
required.append(field.name)
|
39
|
+
else:
|
40
|
+
properties[field.name]["default"] = field.default
|
41
|
+
defs[annotation.__name__] = {"type": "object", "properties": properties, "required": required}
|
42
|
+
return {"$ref": f"#/$defs/{annotation.__name__}"}
|
43
|
+
|
44
|
+
return {"type": type_map[annotation]}
|
45
|
+
|
46
|
+
|
47
|
+
def compile_function_definition(function: Callable) -> FunctionDefinition:
|
48
|
+
"""
|
49
|
+
Converts a Python function into our Vellum-native FunctionDefinition type.
|
50
|
+
"""
|
51
|
+
|
52
|
+
try:
|
53
|
+
signature = inspect.signature(function)
|
54
|
+
except ValueError as e:
|
55
|
+
raise ValueError(f"Failed to get signature for function {function.__name__}: {str(e)}")
|
56
|
+
|
57
|
+
properties = {}
|
58
|
+
required = []
|
59
|
+
defs: dict[str, Any] = {}
|
60
|
+
for param in signature.parameters.values():
|
61
|
+
properties[param.name] = _compile_annotation(param.annotation, defs)
|
62
|
+
if param.default is inspect.Parameter.empty:
|
63
|
+
required.append(param.name)
|
64
|
+
else:
|
65
|
+
properties[param.name]["default"] = param.default
|
66
|
+
|
67
|
+
parameters = {"type": "object", "properties": properties, "required": required}
|
68
|
+
if defs:
|
69
|
+
parameters["$defs"] = defs
|
70
|
+
|
71
|
+
return FunctionDefinition(
|
72
|
+
name=function.__name__,
|
73
|
+
parameters=parameters,
|
74
|
+
)
|
@@ -0,0 +1,171 @@
|
|
1
|
+
from dataclasses import dataclass
|
2
|
+
from typing import Dict, List, Optional, Union
|
3
|
+
|
4
|
+
from vellum.client.types.function_definition import FunctionDefinition
|
5
|
+
from vellum.workflows.utils.functions import compile_function_definition
|
6
|
+
|
7
|
+
|
8
|
+
def test_compile_function_definition__just_name():
|
9
|
+
# GIVEN a function with just a name
|
10
|
+
def my_function():
|
11
|
+
pass
|
12
|
+
|
13
|
+
# WHEN compiling the function
|
14
|
+
compiled_function = compile_function_definition(my_function)
|
15
|
+
|
16
|
+
# THEN it should return the compiled function definition
|
17
|
+
assert compiled_function == FunctionDefinition(
|
18
|
+
name="my_function",
|
19
|
+
parameters={"type": "object", "properties": {}, "required": []},
|
20
|
+
)
|
21
|
+
|
22
|
+
|
23
|
+
def test_compile_function_definition__all_args():
|
24
|
+
# GIVEN a function with args of all base types
|
25
|
+
def my_function(a: str, b: int, c: float, d: bool, e: list, f: dict):
|
26
|
+
pass
|
27
|
+
|
28
|
+
# WHEN compiling the function
|
29
|
+
compiled_function = compile_function_definition(my_function)
|
30
|
+
|
31
|
+
# THEN it should return the compiled function definition
|
32
|
+
assert compiled_function == FunctionDefinition(
|
33
|
+
name="my_function",
|
34
|
+
parameters={
|
35
|
+
"type": "object",
|
36
|
+
"properties": {
|
37
|
+
"a": {"type": "string"},
|
38
|
+
"b": {"type": "integer"},
|
39
|
+
"c": {"type": "number"},
|
40
|
+
"d": {"type": "boolean"},
|
41
|
+
"e": {"type": "array"},
|
42
|
+
"f": {"type": "object"},
|
43
|
+
},
|
44
|
+
"required": ["a", "b", "c", "d", "e", "f"],
|
45
|
+
},
|
46
|
+
)
|
47
|
+
|
48
|
+
|
49
|
+
def test_compile_function_definition__unions():
|
50
|
+
# GIVEN a function with a union arg
|
51
|
+
def my_function(a: Union[str, int]):
|
52
|
+
pass
|
53
|
+
|
54
|
+
# WHEN compiling the function
|
55
|
+
compiled_function = compile_function_definition(my_function)
|
56
|
+
|
57
|
+
# THEN it should return the compiled function definition
|
58
|
+
assert compiled_function == FunctionDefinition(
|
59
|
+
name="my_function",
|
60
|
+
parameters={
|
61
|
+
"type": "object",
|
62
|
+
"properties": {
|
63
|
+
"a": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
|
64
|
+
},
|
65
|
+
"required": ["a"],
|
66
|
+
},
|
67
|
+
)
|
68
|
+
|
69
|
+
|
70
|
+
def test_compile_function_definition__optionals():
|
71
|
+
# GIVEN a function with various ways to specify optionals
|
72
|
+
def my_function(
|
73
|
+
a: str,
|
74
|
+
b: Optional[str],
|
75
|
+
c: None,
|
76
|
+
d: str = "hello",
|
77
|
+
e: Optional[str] = None,
|
78
|
+
):
|
79
|
+
pass
|
80
|
+
|
81
|
+
# WHEN compiling the function
|
82
|
+
compiled_function = compile_function_definition(my_function)
|
83
|
+
|
84
|
+
# THEN it should return the compiled function definition
|
85
|
+
assert compiled_function == FunctionDefinition(
|
86
|
+
name="my_function",
|
87
|
+
parameters={
|
88
|
+
"type": "object",
|
89
|
+
"properties": {
|
90
|
+
"a": {"type": "string"},
|
91
|
+
"b": {"anyOf": [{"type": "string"}, {"type": "null"}]},
|
92
|
+
"c": {"type": "null"},
|
93
|
+
"d": {"type": "string", "default": "hello"},
|
94
|
+
"e": {"anyOf": [{"type": "string"}, {"type": "null"}], "default": None},
|
95
|
+
},
|
96
|
+
"required": ["a", "b", "c"],
|
97
|
+
},
|
98
|
+
)
|
99
|
+
|
100
|
+
|
101
|
+
def test_compile_function_definition__parameterized_dicts():
|
102
|
+
# GIVEN a function with a parameterized dict
|
103
|
+
def my_function(a: Dict[str, int]):
|
104
|
+
pass
|
105
|
+
|
106
|
+
# WHEN compiling the function
|
107
|
+
compiled_function = compile_function_definition(my_function)
|
108
|
+
|
109
|
+
# THEN it should return the compiled function definition
|
110
|
+
assert compiled_function == FunctionDefinition(
|
111
|
+
name="my_function",
|
112
|
+
parameters={
|
113
|
+
"type": "object",
|
114
|
+
"properties": {
|
115
|
+
"a": {"type": "object", "additionalProperties": {"type": "integer"}},
|
116
|
+
},
|
117
|
+
"required": ["a"],
|
118
|
+
},
|
119
|
+
)
|
120
|
+
|
121
|
+
|
122
|
+
def test_compile_function_definition__parameterized_lists():
|
123
|
+
# GIVEN a function with a parameterized list
|
124
|
+
def my_function(a: List[int]):
|
125
|
+
pass
|
126
|
+
|
127
|
+
# WHEN compiling the function
|
128
|
+
compiled_function = compile_function_definition(my_function)
|
129
|
+
|
130
|
+
# THEN it should return the compiled function definition
|
131
|
+
assert compiled_function == FunctionDefinition(
|
132
|
+
name="my_function",
|
133
|
+
parameters={
|
134
|
+
"type": "object",
|
135
|
+
"properties": {
|
136
|
+
"a": {"type": "array", "items": {"type": "integer"}},
|
137
|
+
},
|
138
|
+
"required": ["a"],
|
139
|
+
},
|
140
|
+
)
|
141
|
+
|
142
|
+
|
143
|
+
def test_compile_function_definition__dataclasses():
|
144
|
+
# GIVEN a function with a dataclass
|
145
|
+
@dataclass
|
146
|
+
class MyDataClass:
|
147
|
+
a: int
|
148
|
+
b: str
|
149
|
+
|
150
|
+
def my_function(c: MyDataClass):
|
151
|
+
pass
|
152
|
+
|
153
|
+
# WHEN compiling the function
|
154
|
+
compiled_function = compile_function_definition(my_function)
|
155
|
+
|
156
|
+
# THEN it should return the compiled function definition
|
157
|
+
assert compiled_function == FunctionDefinition(
|
158
|
+
name="my_function",
|
159
|
+
parameters={
|
160
|
+
"type": "object",
|
161
|
+
"properties": {"c": {"$ref": "#/$defs/MyDataClass"}},
|
162
|
+
"required": ["c"],
|
163
|
+
"$defs": {
|
164
|
+
"MyDataClass": {
|
165
|
+
"type": "object",
|
166
|
+
"properties": {"a": {"type": "integer"}, "b": {"type": "string"}},
|
167
|
+
"required": ["a", "b"],
|
168
|
+
}
|
169
|
+
},
|
170
|
+
},
|
171
|
+
)
|
@@ -40,8 +40,8 @@ def primitive_type_to_vellum_variable_type(type_: Union[Type, BaseDescriptor]) -
|
|
40
40
|
int,
|
41
41
|
float,
|
42
42
|
str,
|
43
|
-
typing.List[typing.ForwardRef(
|
44
|
-
typing.Dict[str, typing.ForwardRef(
|
43
|
+
typing.List[typing.ForwardRef("Json")], # type: ignore [misc]
|
44
|
+
typing.Dict[str, typing.ForwardRef("Json")], # type: ignore [misc]
|
45
45
|
]:
|
46
46
|
return "JSON"
|
47
47
|
raise ValueError(f"Expected Descriptor to only have one type, got {types}")
|