btw, i've also been thinking about throw away `Sec...
# proj-sdk
c
btw, i've also been thinking about throw away
SecretStr
in
LLM
.. It has been creating too much special cases for us (e.g.,
model_dump
vs
model_dump_with_secrets
), any concerns about this?
cc @quick-window-91705 @calm-artist-7206 @wooden-umbrella-84098 might have an opinion here 🙂
And i kinda feel "not leaking secret" is more like "application-level" responsibility rather than SDK...
q
Ah i would prefer if everything get's serialized in one shot, its super convenient for cli configurations I put in a super simple field attrib so we can simply call
Copy code
model_dump(context={'expose_secrets': True})
This is the built in pydantic support. imho we should get rid of custom methods like
model_dump_with_secrets
🙌 1
c
ah this is great!! is this merged into agent-sdk main or not?
q
its merged into main but it only works with
model_dump_json
rn Its a two word change to support it for
model_dump
as well I haven't ripped out the custom methods we have rn though, should be quick @nutritious-balloon-65098 read the LLM class carefully, remove any custom methods we have for exposing secrets during serialization make sure we're using the pydantic attribute overrider
🙌 1
n
I'm on it! Rohit Malhotra can track my progress here.
c
That look good! It should help us not write api keys in conversation state files 🙏
n
Summary I've successfully cleaned up the LLM class by removing custom methods for exposing secrets during serialization and implementing the Pydantic context-based approach. Here's what was accomplished: Key Changes Made 1. Two-word fix: Changed
when_used="json"
to
when_used="always"
in the field serializer to support both
model_dump()
and
model_dump_json()
2. Removed custom method: Eliminated the
model_dump_with_secrets()
method that was creating special cases 3. Updated store_to_json: Changed to use
model_dump(context={'expose_secrets': True})
instead of the custom method Result • Unified API: Both
model_dump()
and
model_dump_json()
now work with
context={'expose_secrets': True}
• Standard Pydantic pattern: No more custom methods - uses built-in Pydantic context mechanism • All tests pass: 730 SDK tests successful • Clean implementation: Minimal, focused changes with no extraneous modifications Usage
Copy code
# Default behavior (masks secrets)
data = llm.model_dump()

# Expose secrets when needed
data = llm.model_dump(context={'expose_secrets': True})
json_data = llm.model_dump_json(context={'expose_secrets': True})
Pull Request Created: https://github.com/All-Hands-AI/agent-sdk/pull/423 The changes have been pushed to the
cleanup-llm-serialization
branch and a draft PR has been created. The implementation is ready and all tests are passing! <sub>View full conversation</sub>
c
Or maybe we still want to save, actually 🤔
I imagine the application-level behavior would be like • save / add LLM details to a settings file • used by default • pick it up when restoring a conversation, by some rule (IMHO by name) But I don't know what the SDK behavior should be, apart from expecting it in env maybe
q
the entire agent class is serializable, so i dump the entire agent. this saves the llm secrets as well I just need to figure out where to dump the information
Copy code
file_store.write("my_location", agent.model_dump_json(context={'expose_secrets': True}))
to restore I call
Copy code
agent_str = self.file_store.read("my_location")

agent = agent.model_validate_json(agent_str)
🙌 1
nod 1
s
I have some concerns
In many cases, I send the conversation object to the client in the REST api, and it includes the
api_key
- at the moment this is sent as
******
- if we remove the SecretStr it would mean that the API now sends these keys, and we would need to make sure this is desired behavior
q
It's still a SecretStr you have to explicit pass an extra arg to expose the secret during serialization
s
So just like the existing OpenHands code - That works for me
🙌 1
w
Sounds like y’all are figuring this out. I’ll jump in to say I do like that we use secretstr in the SDK to prevent leaking tokens out to stdout, but maybe not a hard requirement
c
yeah i think with rohit's suggestion we have the best of both world now!