Return classes instead of dicts from nodes in langgraph

Hello there!
is there any way to change return type of nodes in LG ?
i want a more modular way thats actually type safe. ( maybe directly modify state object ? )
Thanks !

@pawel-twardziak
Can you please help me out :slight_smile:
I really appreciate it :folded_hands:

hi @Sanctious

sure, I would love to help :slight_smile: But I am not really sure I understand your aim. Could you explain it a bit more maybe showing some code examples?

Sorry for the ambiguity. What I’m aiming for is type safety and modularity, I don’t want to rely on plain dictionaries with string-based field names and no type checking. Is there a recommended best practice for achieving this in LG?
It makes more sense to return some kind of delta class or structured model for the state instead.
Thanks!

Ok, thanks @Sanctious .

I think you could explicitly define the return type of your nodes. You are only referring to the nodes’ return type, right?

def node_b(state: State):
        return {
            "now": 123,
            "hello": "again",
        }

I meant sth like this. Is there any way to implement it better ?

you want that return type to be type safe in terms of the state it is gonna be merged into?

yes :slight_smile:

I get it :slight_smile:

have you seen that docs Graph API overview - Docs by LangChain and Use the graph API - Docs by LangChain ?

1 Like

Damn
I missed this part of the docs entirely :joy:
Thanks mate !

1 Like

@pawel-twardziak Btw is there a way to have updates returned as DeltaClass instances instead of plain dicts?
For example, when defining a data transmission scheme, it also specifies a corresponding DeltaClass that encapsulates its updates, rather than using a raw dict.

Hi @Sanctious

what do you mean by DeltaClass? Unfortunately I dunno what that is.

Ummm
I didn’t mean it literally, I meant it conceptually.
a class that contains the same exact fields as the original one but its used to represent changes and applying them to the original class.

class A:
   a: int
   b: str
class dA:
    da: int
    db: str

Ok, I get it. I think that would require some changes to the langgraph repo itself, right?

1 Like

Thats what i was wondering about. I thought there was a built-in feature that allowed this.
Sad.
Thanks for your time ! :heart:

1 Like

You can create your own state reducers to do what youre looking.

1 Like