Hey there Eden here.
And in this video we're going to be discussing structured output.
So most of the time when we're going to be building agents and AI applications, we are going to be
using lens and lens respond in text.
Now if we want to take this text and we want to create a structure to it, and we want to downstream
it into an application, maybe to serialize it, maybe to render it in the user interface, then we
need to have structured output.
So we don't just want text, we want maybe a JSON object or maybe a Pydantic object that we can go and
we can downstream to our application and we can programmatically parse and work with.
And this is what we're going to be doing in this video.
And LinkedIn provides us a very easy way to do this with the create agent function.
We need to simply add an argument which is called response format.
And we're going to provide to this argument the schema that we want.
It may be a JSON schema.
It may be a Pydantic object.
And then magically the response that the agent is going to output us, is going to adhere to that scheme
or to that data object.
So in this video, we're going to be creating a Pydantic object, and we're going to be creating a nested
Pydantic object just to add a bit of complexity to what we're building.
So the Pydantic object is going to be our agent response.
And it's going to be the answer of the agent.
So this is going to be one field.
And it's also going to have a sources field which is going to be a list of sources Pydantic object as
well, which is going to represent the URL where the agent took the answer from.
So this is going to be where the agent grounded its answer from.
And I want to remind you that in this video we're just reviewing interface.
So we're going to see how we can use it.
And to use the technology.
And in the next section we're going to see how this is implemented under the hood.
So let's go to the code.
Let me go and import from typing the list type int.
And I also want to import from pedantic.
The base model class and the field class.
And for those of you who are not familiar with pedantic, pedantic base model is going to give us a
base class so we can inherit from in order to define a structured data scheme.
So it's going to provide functionality like data parsing and serialization and automatic type validations.
And the field class is going to allow us to add metadata to our models attribute.
So we can add descriptions, which is going to be helpful for the Llms to understand what to put in
that field.
All right.
So let me now go and create my pedantic object.
And I want to start by writing an object to describe the sources that the agent use to get the answers.
So I'm going to define a new class and I'm going to call it source.
It's going to inherit from the pedantic base model.
And let me write here in the description.
Scheme for a source used by the agent.
So now let me go and define the field.
Let's call this field URL.
And it's going to be a string.
And here I'm going to use the field class.
And I can add here a description.
And here in the description I'm going to write the URL of the source.
So the point of this class here is to represent the source of the answer.
Now I want this source pedantic class to be a nested field in another pedantic class, which is going
to be called agent response.
So agent response is going to have a an answer, and it's going to have a list of a sources.
All right.
So let's go and create this agent response class.
So I'm going to write class agent response.
And it's going to also inherit from base model.
And let's go and add the description for this class.
So this is going to be the scheme for the agent response.
So it's going to have an answer and it's going to have the sources.
So let me now add the attribute of an answer which is going to be a string.
And let me write in the description that this is going to be the agent's answer to the query.
I have a typo here, but don't worry, the LLM can handle it easily.
So the second attribute of this class is going to be the sources.
So let me write here sources.
And sources is going to be a list.
And it's going to be a list of source.
Now let me create a field class.
And here let me write that the default factory is going to be list.
And this will make that when we create an agent response class.
If we don't provide the sources, then the sources attribute is going to be populated with an empty
list.
And let's add the description here.
And here is going to be the list of sources used to generate the answer.
Alrighty.
So now we want our agent to not return us a string like we saw before, but we want our agent to return
us an agent response object so we can then downstream it into an application.
Maybe we want to serialize it and then return it in a response from a server.
And then we want to render this response in the front end.
So we want our agent to return this data structure.
And to do that all we need to do is add to the create agent function the argument of response format.
And we want the response format to be the agent response class that we created earlier.
And that's it.
It's going to work like magic.
And the agent response now is going to be of type agent response here.
So it's going to have all the fields that we requested.
And this is going to work really really well.
And we can downstream it into an application.
Okay.
So let's go and run it now.
And let me show you how it's going to be looking like.
So I'm going to run it in debug mode.
And I'm going to put a breakpoint right before we print it.
And let me fast forward it a bit.
And now in the result we have now a new key here which is called structured response here.
And the type of structured response is going to be agent response.
So it's going to have the answer field and it's going to have the sources field which is going to be
a list of sources.
Right.
So let me go now to the debug console and let me show you this in another eye.
So let's get the type of results in the key of Structured response.
So it's going to be agent response class.
So let me go and print it.
So let's go and sorry.
Let's go and print this um this variable here.
So now we can see all the information we have here the answer field.
So let's go check this out.
And this is the answer.
And let's go and check out the sources so we can see the sources is a list.
So let's go and check one element of the list.
Let's go to that URL.
Let's open it.
We can see this is a job application which is not actually accepting candidates.
But still it's looking for link chain developers.
So this is how to use the response format argument in the link chain agent.
So it's going to return us a structured response here.
Right.
So right now this works like a black box, so we do not know how it's working under the hood.
We are going to figure this out in the next section.
So right now I want to open the trace and show you how it's going to look there.
So let me go and open blacksmith and let me go to the last trace here.
And we can see.
Let me go to the very bottom here to the last, last result here.
So we can see we have this additional field.
We have a structured response.
So it's going to have the answer field.
And this is going to contain the answer.
And then we're going to have the sources field which is going to be a list of source objects.
And each object has the URL field which is going to contain the LinkedIn URL for the job posting.
Okay.
So very similar to what we saw in the debugger.
So now we're seeing it in the trace.
Now how is this working under the hood?
I'm going to give you a quick hint.
It's going to be using function calling.
But more on this on the next couple of sections.