WEBVTT

1
00:00:00.150 --> 00:00:01.320
<v Ethan>Hey there, Ethan here.</v>

2
00:00:01.320 --> 00:00:03.420
And I want in this video

3
00:00:03.420 --> 00:00:06.210
to dive a bit deeper about the implementation

4
00:00:06.210 --> 00:00:07.650
of structured output.

5
00:00:07.650 --> 00:00:09.240
So as we saw in the course,

6
00:00:09.240 --> 00:00:11.790
structured output allows agents

7
00:00:11.790 --> 00:00:16.410
to return data in specific, predictable format.

8
00:00:16.410 --> 00:00:20.880
So instead of getting a raw-text answer,

9
00:00:20.880 --> 00:00:24.000
we can get a Pydantic object,

10
00:00:24.000 --> 00:00:26.040
we can get a JSON response,

11
00:00:26.040 --> 00:00:27.900
or a dataclass,

12
00:00:27.900 --> 00:00:31.410
and something which is downstreamable to our application

13
00:00:31.410 --> 00:00:32.910
so we can use later.

14
00:00:32.910 --> 00:00:35.280
Now, we saw how we can use it

15
00:00:35.280 --> 00:00:38.670
in the create a agent function, right?

16
00:00:38.670 --> 00:00:42.450
Now, there are a bunch of implementations

17
00:00:42.450 --> 00:00:44.220
to structured output.

18
00:00:44.220 --> 00:00:46.770
So using structured output

19
00:00:46.770 --> 00:00:50.880
has two different types of implementation.

20
00:00:50.880 --> 00:00:53.700
The first one is tool strategy,

21
00:00:53.700 --> 00:00:57.240
which is going to use tool calling under the hood,

22
00:00:57.240 --> 00:01:00.420
and the second is called provider strategy.

23
00:01:00.420 --> 00:01:02.010
And this is going to be using

24
00:01:02.010 --> 00:01:07.010
the native model structured output capabilities.

25
00:01:07.230 --> 00:01:11.880
Now, LangChain is going to choose by default

26
00:01:11.880 --> 00:01:13.890
to use the provider strategy,

27
00:01:13.890 --> 00:01:15.930
unless we specify otherwise.

28
00:01:15.930 --> 00:01:19.440
So let's start talking about the provider strategy.

29
00:01:19.440 --> 00:01:21.900
So most top-tier models,

30
00:01:21.900 --> 00:01:26.160
they offer support for structured output natively.

31
00:01:26.160 --> 00:01:29.370
So this means we can use their own APIs

32
00:01:29.370 --> 00:01:33.060
and use a structured output argument,

33
00:01:33.060 --> 00:01:35.580
and they're going to do all the heavy loading

34
00:01:35.580 --> 00:01:38.370
and they're going to make the response

35
00:01:38.370 --> 00:01:40.020
be in the format that we want.

36
00:01:40.020 --> 00:01:42.930
And we can see that it's going to receive

37
00:01:42.930 --> 00:01:44.250
a bunch of schema types.

38
00:01:44.250 --> 00:01:46.757
So it can be a Pydantic model, a dataclass,

39
00:01:46.757 --> 00:01:49.500
a TypedDict and JSON scheme.

40
00:01:49.500 --> 00:01:52.290
We can see it right over here all the examples.

41
00:01:52.290 --> 00:01:54.480
This is for a Pydantic model,

42
00:01:54.480 --> 00:01:56.280
this is for a dataclass,

43
00:01:56.280 --> 00:01:57.660
this is for a TypedDict,

44
00:01:57.660 --> 00:01:59.940
and this is for a JOSN scheme.

45
00:01:59.940 --> 00:02:01.290
And by default,

46
00:02:01.290 --> 00:02:04.230
if the model we're going to be using in the create agent

47
00:02:04.230 --> 00:02:07.590
is going to support structured output,

48
00:02:07.590 --> 00:02:09.750
then LangChain is going to be using that,

49
00:02:09.750 --> 00:02:10.920
because by doing it,

50
00:02:10.920 --> 00:02:13.110
they're shifting all of the responsibility

51
00:02:13.110 --> 00:02:14.760
to the model provider

52
00:02:14.760 --> 00:02:16.470
to return us a good answer.

53
00:02:16.470 --> 00:02:20.325
And this basically means that if we get a wrong answer,

54
00:02:20.325 --> 00:02:23.490
(laughing) we can talk to the vendor and not LangChain.

55
00:02:23.490 --> 00:02:28.490
Now, some models do not offer this structured output API.

56
00:02:29.250 --> 00:02:32.370
In case the model and all of the top-tier models

57
00:02:32.370 --> 00:02:33.990
supports tool calling,

58
00:02:33.990 --> 00:02:35.730
we have a workaround to do it,

59
00:02:35.730 --> 00:02:38.070
and this is actually how everything evolved.

60
00:02:38.070 --> 00:02:40.860
So we can actually use tool calling

61
00:02:40.860 --> 00:02:43.020
and LangChain under the hood

62
00:02:43.020 --> 00:02:44.400
uses tool calling

63
00:02:44.400 --> 00:02:47.190
with the schema of the object we want

64
00:02:47.190 --> 00:02:51.900
in providing one single tool to the LLM

65
00:02:51.900 --> 00:02:53.610
and saying to the LLM

66
00:02:53.610 --> 00:02:55.770
they always need to choose that tool.

67
00:02:55.770 --> 00:02:56.910
And by doing that,

68
00:02:56.910 --> 00:02:58.830
we are forcing the LLM

69
00:02:58.830 --> 00:03:01.590
to enforce the schema that we send it.

70
00:03:01.590 --> 00:03:04.080
So this is a nice workaround

71
00:03:04.080 --> 00:03:05.910
to get structured output here.

72
00:03:05.910 --> 00:03:08.820
So LangChain now implement this for us.

73
00:03:08.820 --> 00:03:09.720
And in this case,

74
00:03:09.720 --> 00:03:12.241
it also supports all the Pydantic models,

75
00:03:12.241 --> 00:03:15.573
dataclass, TypedDict and JSON scheme.

