AE
Ash Elixir•3w ago
smelly

Return only selected fields

How do i return only the selected fields instead of the whole struct? Cant seem to find a good example for the action based version
read :select_specific_fields do
prepare build(
select: [:id, :name]
)

pagination do
default_limit 50
offset? true
countable true
end
end
read :select_specific_fields do
prepare build(
select: [:id, :name]
)

pagination do
default_limit 50
offset? true
countable true
end
end
16 Replies
ZachDaniel
ZachDaniel•3w ago
"return" in what context? over an API? Elixir structs always have all fields present, so there is no way to literally remove those fields from the structs
smelly
smellyOP•3w ago
My brain is still working in sql, yes over an API. how do I get reduce the amount of unneeded data getting received when I make an api call over even use the code interface method.
ZachDaniel
ZachDaniel•3w ago
You'd do that in the api layer With ash_json_api that's default_fields
kira🌺
kira🌺•3w ago
if you use ash_json_api, you can do this
json_api do
type "your_type"
default_fields [:id, :name] # add this
end
json_api do
type "your_type"
default_fields [:id, :name] # add this
end
this is an example if your json_api is defined on your resource i suppose :id is your primary key? if so, then you possibly don't need it inside data[*].attributes of the response. it's already present in data[*].id
smelly
smellyOP•3w ago
Perfect. Then what happens on ash_ai when the tool is called?
ZachDaniel
ZachDaniel•3w ago
You don't need to do selects in the resource if you do it in the default select, and then the tool action will select everything by default
smelly
smellyOP•3w ago
nice, now can the default select only apply to certain actions or does it have to apply to all? just trying to minimize the amount of unneeded data being returned
ZachDaniel
ZachDaniel•3w ago
You can put default_select on specific routes in ash_json_api
smelly
smellyOP•3w ago
man still not getting it. this is in the domain?
json_api do
routes do
base_route "/artists", Tunez.Music.Artist do
get :read
index :return_default_fields do
default_fields [:name, :genre]
end
end
end
end
json_api do
routes do
base_route "/artists", Tunez.Music.Artist do
get :read
index :return_default_fields do
default_fields [:name, :genre]
end
end
end
end
or
json_api do
routes do
base_route "/artists", Tunez.Music.Artist do
get :read
end
base_route "/artists/default_fields", Tunez.Music.Artist do
index :return_default_fields do
default_fields [:name, :genre]
end
end
end
end
json_api do
routes do
base_route "/artists", Tunez.Music.Artist do
get :read
end
base_route "/artists/default_fields", Tunez.Music.Artist do
index :return_default_fields do
default_fields [:name, :genre]
end
end
end
end
ZachDaniel
ZachDaniel•3w ago
🤔 You don't need to use a different action or anything like that The default fields is automatically applied to the read action as a select or a load etc
smelly
smellyOP•3w ago
Thanks Zach, if i have multiple read actions and want one to return the full query but the other one to only return the default fields is that possible? Currently i have only found im am able to apply default fields to all actions. Using this with tools in ash ai and have specific actions that i want to not return unneeded data to consume the context window. So just looking for a good way to do specific selects
ZachDaniel
ZachDaniel•3w ago
I'm confused TBH. What does AshAi have to do with ash_json_api? There is no current selecting feature for ash ai Oh interesting...is ash_json_api's default fields somehow impacting ash_ai? If so that's a bug 😅
smelly
smellyOP•3w ago
Yeah think Im misunderstanding as well. If i do the following on the resource it applies to all read actions and returns only those fields for the different reads using the tools. I have tried a bunch of different things, adding separate routes in the resource, in the domain, and adding default fields to those routes and they dont seem to apply. when using a tool I can check the json endpoint and get the correct default fields at say artists/default_fields
json_api do
type "artist"
default_fields [:name, :genre]
end
json_api do
type "artist"
default_fields [:name, :genre]
end
ZachDaniel
ZachDaniel•3w ago
That's a bug 🙂 AshAi should not be using that config It's a byproduct of us reusing the ash json api serializer The reason changing other things doesn't work is because AshAi isn't using routes etc, it just fakes it for serialization. Please open an issue on ash_ai. The first step will be temoving that behavior And then we can talk about how to limit fields Probably what we'd do first is actually just allow the agent to specify a select, or maybe rework the load logic to work such that we default to selecting nothing and make the default load statement all public attributes
smelly
smellyOP•3w ago
Oh interesting idea to have the agent specify., Initial thought was to have a return format on the tool call
ZachDaniel
ZachDaniel•3w ago
I think we'd probably do both in the long run

Did you find this page helpful?