Skip to content

Commit

Permalink
Fix typespec generation for response oneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
dvcrn committed Jul 13, 2024
1 parent be7cfde commit 02e7f0a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/ex_openai.ex
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,17 @@ end)
merged_required_args
|> Enum.map(fn item -> quote do: unquote(ExOpenAI.Codegen.type_to_spec(item.type)) end)

# convert optional args into keyword list
response_spec = ExOpenAI.Codegen.type_to_spec(response_type)
# construct response spec
# for list types, instead of {:ok, list1 | list2}, we want {:ok, list1} | {:ok, list2}
response_spec =
case response_type do
{:oneOf, c} ->
Enum.map(c, fn comp -> {:ok, ExOpenAI.Codegen.type_to_spec(comp)} end)
|> Enum.reduce(&{:|, [], [&1, &2]})

etc ->
{:ok, ExOpenAI.Codegen.type_to_spec(etc)}
end

optional_args =
merged_optional_args
Expand Down Expand Up @@ -308,11 +317,11 @@ end)

# fx without opts
@spec unquote(name)(unquote_splicing(spec)) ::
{:ok, unquote(response_spec)} | {:error, any()}
unquote(response_spec) | {:error, any()}

# fx with opts
@spec unquote(name)(unquote_splicing(spec), unquote(optional_args)) ::
{:ok, unquote(response_spec)} | {:error, any()}
unquote(response_spec) | {:error, any()}

def unquote(name)(unquote_splicing(arg_names), opts \\ []) do
# store binding so we can't access args of the function later
Expand Down

0 comments on commit 02e7f0a

Please sign in to comment.