vkryukov

vkryukov

Can Ash calculation return a map?

Hi, I have the following calculation in one of my resources:

  calculations do
    calculate :llm_model,
              :map,
              expr(
                cond do
                  role == :basic -> %{"model" => "gpt-4o-mini"}
                  role == :pro -> %{"model" => "o3-mini", "reasoning_effort" => "medium"}
                  role == :admin -> %{"model" => "o3-mini", "reasoning_effort" => "high"}
                end
              )
  end

This compiles without any problems, and my reading of Ash.Resource — ash v3.29.3 is that any return type, including :map, is allowed.

However, I get the following error when executing this code, where victor is a User resource:

victor |> Ash.load!(:llm_model)
[debug] QUERY ERROR source="users" db=2.3ms queue=1.4ms idle=1593.8ms
SELECT u0."id", (CASE WHEN u0."role"::varchar::varchar = $1::varchar::varchar THEN $2 WHEN u0."role"::varchar::varchar = $3::varchar::varchar THEN $4 WHEN u0."role"::varchar::varchar = $5::varchar::varchar THEN $6 ELSE NULL END) FROM "users" AS u0 WHERE (u0."id"::uuid::uuid = $7::uuid::uuid) [:basic, %{"model" => "gpt-4o-mini"}, :pro, %{"model" => "o3-mini", "reasoning_effort" => "medium"}, :admin, %{"model" => "o3-mini", "reasoning_effort" => "high"}, "ab2ba555-671f-4d73-861d-c9171ec7ec9b"]
↳ anonymous fn/3 in AshPostgres.DataLayer.run_query/2, at: lib/data_layer.ex:785
** (Ash.Error.Unknown)
Bread Crumbs:
  > Error returned from: FiveWhys.Accounts.User.read

Unknown Error

* ** (DBConnection.EncodeError) Postgrex expected a binary, got %{"model" => "gpt-4o-mini"}. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.
<...>

I understand it that there is an attempt to evaluate the expression in Postgresql level, which understandably fails.

Am I expecting too much from calculated fields here? I can of course split it into two calculated fields, e.g., model and reasoning_effort, but I’m curious what is the idiomatic Ash solution here.

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

This is a bug. Can you open an issue on the ash_postgres repo with a link to a reproduction?

Also Liked

vkryukov

vkryukov

Yes, upgraded everything and can confirm that the bug is still there:

Upgraded:
  ash 3.5.2 => 3.5.4
  ash_admin 0.13.3 => 0.13.4
  ash_authentication 4.6.3 => 4.6.4
  ash_authentication_phoenix 2.5.2 => 2.5.3
  ash_graphql 1.7.6 => 1.7.7
  ash_phoenix 2.1.25 => 2.1.26
  ash_postgres 2.5.14 => 2.5.15
  ash_sql 0.2.66 => 0.2.67

Created a ticket: Calculated field with :map return type doesn't work with Postgres backend · Issue #528 · ash-project/ash_postgres · GitHub

Last Post!

vkryukov

vkryukov

Yes, upgraded everything and can confirm that the bug is still there:

Upgraded:
  ash 3.5.2 => 3.5.4
  ash_admin 0.13.3 => 0.13.4
  ash_authentication 4.6.3 => 4.6.4
  ash_authentication_phoenix 2.5.2 => 2.5.3
  ash_graphql 1.7.6 => 1.7.7
  ash_phoenix 2.1.25 => 2.1.26
  ash_postgres 2.5.14 => 2.5.15
  ash_sql 0.2.66 => 0.2.67

Created a ticket: Calculated field with :map return type doesn't work with Postgres backend · Issue #528 · ash-project/ash_postgres · GitHub

Where Next?

Popular in Questions Top

New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement