langfun 0.1.2.dev202510230805__py3-none-any.whl → 0.1.2.dev202511270805__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of langfun might be problematic. Click here for more details.

Files changed (155) hide show
  1. langfun/core/__init__.py +2 -0
  2. langfun/core/agentic/__init__.py +4 -1
  3. langfun/core/agentic/action.py +447 -29
  4. langfun/core/agentic/action_eval.py +9 -2
  5. langfun/core/agentic/action_test.py +149 -21
  6. langfun/core/async_support.py +32 -3
  7. langfun/core/coding/python/correction.py +19 -9
  8. langfun/core/coding/python/execution.py +14 -12
  9. langfun/core/coding/python/generation.py +21 -16
  10. langfun/core/coding/python/sandboxing.py +23 -3
  11. langfun/core/component.py +42 -3
  12. langfun/core/concurrent.py +70 -6
  13. langfun/core/concurrent_test.py +1 -0
  14. langfun/core/console.py +1 -1
  15. langfun/core/data/conversion/anthropic.py +12 -3
  16. langfun/core/data/conversion/anthropic_test.py +8 -6
  17. langfun/core/data/conversion/gemini.py +9 -2
  18. langfun/core/data/conversion/gemini_test.py +12 -9
  19. langfun/core/data/conversion/openai.py +145 -31
  20. langfun/core/data/conversion/openai_test.py +161 -17
  21. langfun/core/eval/base.py +47 -43
  22. langfun/core/eval/base_test.py +5 -5
  23. langfun/core/eval/matching.py +5 -2
  24. langfun/core/eval/patching.py +3 -3
  25. langfun/core/eval/scoring.py +4 -3
  26. langfun/core/eval/v2/__init__.py +1 -0
  27. langfun/core/eval/v2/checkpointing.py +64 -6
  28. langfun/core/eval/v2/checkpointing_test.py +9 -2
  29. langfun/core/eval/v2/eval_test_helper.py +103 -2
  30. langfun/core/eval/v2/evaluation.py +91 -16
  31. langfun/core/eval/v2/evaluation_test.py +9 -3
  32. langfun/core/eval/v2/example.py +50 -40
  33. langfun/core/eval/v2/example_test.py +16 -8
  34. langfun/core/eval/v2/experiment.py +74 -8
  35. langfun/core/eval/v2/experiment_test.py +19 -0
  36. langfun/core/eval/v2/metric_values.py +31 -3
  37. langfun/core/eval/v2/metric_values_test.py +32 -0
  38. langfun/core/eval/v2/metrics.py +157 -44
  39. langfun/core/eval/v2/metrics_test.py +39 -18
  40. langfun/core/eval/v2/progress.py +30 -1
  41. langfun/core/eval/v2/progress_test.py +27 -0
  42. langfun/core/eval/v2/progress_tracking.py +12 -3
  43. langfun/core/eval/v2/progress_tracking_test.py +6 -1
  44. langfun/core/eval/v2/reporting.py +90 -71
  45. langfun/core/eval/v2/reporting_test.py +24 -6
  46. langfun/core/eval/v2/runners/__init__.py +30 -0
  47. langfun/core/eval/v2/{runners.py → runners/base.py} +59 -142
  48. langfun/core/eval/v2/runners/beam.py +341 -0
  49. langfun/core/eval/v2/runners/beam_test.py +131 -0
  50. langfun/core/eval/v2/runners/ckpt_monitor.py +294 -0
  51. langfun/core/eval/v2/runners/ckpt_monitor_test.py +162 -0
  52. langfun/core/eval/v2/runners/debug.py +40 -0
  53. langfun/core/eval/v2/runners/debug_test.py +76 -0
  54. langfun/core/eval/v2/runners/parallel.py +100 -0
  55. langfun/core/eval/v2/runners/parallel_test.py +95 -0
  56. langfun/core/eval/v2/runners/sequential.py +47 -0
  57. langfun/core/eval/v2/runners/sequential_test.py +172 -0
  58. langfun/core/langfunc.py +45 -130
  59. langfun/core/langfunc_test.py +7 -5
  60. langfun/core/language_model.py +141 -21
  61. langfun/core/language_model_test.py +54 -3
  62. langfun/core/llms/__init__.py +9 -1
  63. langfun/core/llms/anthropic.py +157 -2
  64. langfun/core/llms/azure_openai.py +29 -17
  65. langfun/core/llms/cache/base.py +25 -3
  66. langfun/core/llms/cache/in_memory.py +48 -7
  67. langfun/core/llms/cache/in_memory_test.py +14 -4
  68. langfun/core/llms/compositional.py +25 -1
  69. langfun/core/llms/deepseek.py +30 -2
  70. langfun/core/llms/fake.py +32 -1
  71. langfun/core/llms/gemini.py +55 -17
  72. langfun/core/llms/gemini_test.py +84 -0
  73. langfun/core/llms/google_genai.py +34 -1
  74. langfun/core/llms/groq.py +28 -3
  75. langfun/core/llms/llama_cpp.py +23 -4
  76. langfun/core/llms/openai.py +36 -3
  77. langfun/core/llms/openai_compatible.py +148 -27
  78. langfun/core/llms/openai_compatible_test.py +207 -20
  79. langfun/core/llms/openai_test.py +0 -2
  80. langfun/core/llms/rest.py +12 -1
  81. langfun/core/llms/vertexai.py +58 -8
  82. langfun/core/logging.py +1 -1
  83. langfun/core/mcp/client.py +77 -22
  84. langfun/core/mcp/client_test.py +8 -35
  85. langfun/core/mcp/session.py +94 -29
  86. langfun/core/mcp/session_test.py +54 -0
  87. langfun/core/mcp/tool.py +151 -22
  88. langfun/core/mcp/tool_test.py +197 -0
  89. langfun/core/memory.py +1 -0
  90. langfun/core/message.py +160 -55
  91. langfun/core/message_test.py +65 -81
  92. langfun/core/modalities/__init__.py +8 -0
  93. langfun/core/modalities/audio.py +21 -1
  94. langfun/core/modalities/image.py +19 -1
  95. langfun/core/modalities/mime.py +64 -3
  96. langfun/core/modalities/mime_test.py +11 -0
  97. langfun/core/modalities/pdf.py +19 -1
  98. langfun/core/modalities/video.py +21 -1
  99. langfun/core/modality.py +167 -29
  100. langfun/core/modality_test.py +42 -12
  101. langfun/core/natural_language.py +1 -1
  102. langfun/core/sampling.py +4 -4
  103. langfun/core/sampling_test.py +20 -4
  104. langfun/core/structured/__init__.py +2 -24
  105. langfun/core/structured/completion.py +34 -44
  106. langfun/core/structured/completion_test.py +23 -43
  107. langfun/core/structured/description.py +54 -50
  108. langfun/core/structured/function_generation.py +29 -12
  109. langfun/core/structured/mapping.py +81 -37
  110. langfun/core/structured/parsing.py +95 -79
  111. langfun/core/structured/parsing_test.py +0 -3
  112. langfun/core/structured/querying.py +215 -142
  113. langfun/core/structured/querying_test.py +65 -29
  114. langfun/core/structured/schema/__init__.py +49 -0
  115. langfun/core/structured/schema/base.py +664 -0
  116. langfun/core/structured/schema/base_test.py +531 -0
  117. langfun/core/structured/schema/json.py +174 -0
  118. langfun/core/structured/schema/json_test.py +121 -0
  119. langfun/core/structured/schema/python.py +316 -0
  120. langfun/core/structured/schema/python_test.py +410 -0
  121. langfun/core/structured/schema_generation.py +33 -14
  122. langfun/core/structured/scoring.py +47 -36
  123. langfun/core/structured/tokenization.py +26 -11
  124. langfun/core/subscription.py +2 -2
  125. langfun/core/template.py +174 -49
  126. langfun/core/template_test.py +123 -17
  127. langfun/env/__init__.py +8 -2
  128. langfun/env/base_environment.py +320 -128
  129. langfun/env/base_environment_test.py +473 -0
  130. langfun/env/base_feature.py +92 -15
  131. langfun/env/base_feature_test.py +228 -0
  132. langfun/env/base_sandbox.py +84 -361
  133. langfun/env/base_sandbox_test.py +1235 -0
  134. langfun/env/event_handlers/__init__.py +1 -1
  135. langfun/env/event_handlers/chain.py +233 -0
  136. langfun/env/event_handlers/chain_test.py +253 -0
  137. langfun/env/event_handlers/event_logger.py +95 -98
  138. langfun/env/event_handlers/event_logger_test.py +21 -21
  139. langfun/env/event_handlers/metric_writer.py +225 -140
  140. langfun/env/event_handlers/metric_writer_test.py +23 -6
  141. langfun/env/interface.py +854 -40
  142. langfun/env/interface_test.py +112 -2
  143. langfun/env/load_balancers_test.py +23 -2
  144. langfun/env/test_utils.py +126 -84
  145. {langfun-0.1.2.dev202510230805.dist-info → langfun-0.1.2.dev202511270805.dist-info}/METADATA +1 -1
  146. langfun-0.1.2.dev202511270805.dist-info/RECORD +215 -0
  147. langfun/core/eval/v2/runners_test.py +0 -343
  148. langfun/core/structured/schema.py +0 -987
  149. langfun/core/structured/schema_test.py +0 -982
  150. langfun/env/base_test.py +0 -1481
  151. langfun/env/event_handlers/base.py +0 -350
  152. langfun-0.1.2.dev202510230805.dist-info/RECORD +0 -195
  153. {langfun-0.1.2.dev202510230805.dist-info → langfun-0.1.2.dev202511270805.dist-info}/WHEEL +0 -0
  154. {langfun-0.1.2.dev202510230805.dist-info → langfun-0.1.2.dev202511270805.dist-info}/licenses/LICENSE +0 -0
  155. {langfun-0.1.2.dev202510230805.dist-info → langfun-0.1.2.dev202511270805.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,228 @@
1
+ # Copyright 2025 The Langfun Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ import unittest
15
+
16
+ from langfun.env import test_utils
17
+
18
+ TestingEnvironment = test_utils.TestingEnvironment
19
+ TestingNonSandboxBasedFeature = test_utils.TestingNonSandboxBasedFeature
20
+ TestingEventHandler = test_utils.TestingEventHandler
21
+
22
+
23
+ class NonSandboxBasedFeatureTests(unittest.TestCase):
24
+
25
+ def test_basics(self):
26
+ feature = TestingNonSandboxBasedFeature()
27
+ event_handler = TestingEventHandler(
28
+ log_session_setup=True,
29
+ log_feature_setup=True,
30
+ log_sandbox_status=True
31
+ )
32
+ env = TestingEnvironment(
33
+ image_ids=[],
34
+ features={'test_feature': feature},
35
+ event_handler=event_handler,
36
+ )
37
+ self.assertFalse(env.is_online)
38
+ self.assertEqual(len(list(env.non_sandbox_based_features())), 1)
39
+ with env:
40
+ self.assertTrue(env.is_online)
41
+ with env.test_feature('session1') as feature:
42
+ self.assertIsNone(feature.sandbox)
43
+ self.assertEqual(feature.session_id, 'session1')
44
+
45
+ self.assertEqual(
46
+ event_handler.logs,
47
+ [
48
+ '[testing-env/test_feature] feature setup',
49
+ '[testing-env] environment started',
50
+ '[testing-env/test_feature@session1] feature setup session',
51
+ '[testing-env/test_feature@session1] feature teardown session',
52
+ '[testing-env/test_feature] feature teardown',
53
+ '[testing-env] environment shutdown'
54
+ ]
55
+ )
56
+
57
+ def test_feature_setup_error(self):
58
+ event_handler = TestingEventHandler(
59
+ log_session_setup=True,
60
+ log_feature_setup=True,
61
+ log_sandbox_status=True
62
+ )
63
+ env = TestingEnvironment(
64
+ image_ids=[],
65
+ features={
66
+ 'test_feature': TestingNonSandboxBasedFeature(
67
+ simulate_setup_error=ValueError
68
+ )
69
+ },
70
+ event_handler=event_handler,
71
+ )
72
+ with self.assertRaises(ValueError):
73
+ with env:
74
+ pass
75
+ self.assertEqual(
76
+ event_handler.logs,
77
+ [
78
+ '[testing-env/test_feature] feature setup with ValueError',
79
+ '[testing-env] environment started with ValueError',
80
+ '[testing-env/test_feature] feature teardown',
81
+ '[testing-env] environment shutdown'
82
+ ]
83
+ )
84
+
85
+ def test_feature_teardown_error(self):
86
+ event_handler = TestingEventHandler(
87
+ log_session_setup=True,
88
+ log_feature_setup=True,
89
+ log_sandbox_status=True
90
+ )
91
+ env = TestingEnvironment(
92
+ image_ids=[],
93
+ features={
94
+ 'test_feature': TestingNonSandboxBasedFeature(
95
+ simulate_teardown_error=ValueError
96
+ )
97
+ },
98
+ event_handler=event_handler,
99
+ )
100
+ with env:
101
+ pass
102
+ self.assertEqual(
103
+ event_handler.logs,
104
+ [
105
+ '[testing-env/test_feature] feature setup',
106
+ '[testing-env] environment started',
107
+ '[testing-env/test_feature] feature teardown with ValueError',
108
+ '[testing-env] environment shutdown'
109
+ ]
110
+ )
111
+
112
+ def test_feature_setup_session_error(self):
113
+ event_handler = TestingEventHandler(
114
+ log_session_setup=True,
115
+ log_feature_setup=True,
116
+ log_sandbox_status=True
117
+ )
118
+ env = TestingEnvironment(
119
+ image_ids=[],
120
+ features={
121
+ 'test_feature': TestingNonSandboxBasedFeature(
122
+ simulate_setup_session_error=ValueError
123
+ )
124
+ },
125
+ event_handler=event_handler,
126
+ )
127
+ with env:
128
+ with self.assertRaises(ValueError):
129
+ with env.test_feature('session1'):
130
+ pass
131
+ self.assertEqual(
132
+ event_handler.logs,
133
+ [
134
+ # pylint: disable=line-too-long
135
+ '[testing-env/test_feature] feature setup',
136
+ '[testing-env] environment started',
137
+ '[testing-env/test_feature@session1] feature setup session with ValueError',
138
+ '[testing-env/test_feature@session1] feature teardown session',
139
+ '[testing-env/test_feature] feature teardown',
140
+ '[testing-env] environment shutdown',
141
+ # pylint: enable=line-too-long
142
+ ]
143
+ )
144
+
145
+ def test_feature_teardown_session_error(self):
146
+ event_handler = TestingEventHandler(
147
+ log_session_setup=True,
148
+ log_feature_setup=True,
149
+ log_sandbox_status=True
150
+ )
151
+ env = TestingEnvironment(
152
+ image_ids=[],
153
+ features={
154
+ 'test_feature': TestingNonSandboxBasedFeature(
155
+ simulate_teardown_session_error=ValueError
156
+ )
157
+ },
158
+ event_handler=event_handler,
159
+ )
160
+ with env:
161
+ with env.test_feature('session1'):
162
+ pass
163
+ self.assertEqual(
164
+ event_handler.logs,
165
+ [
166
+ # pylint: disable=line-too-long
167
+ '[testing-env/test_feature] feature setup',
168
+ '[testing-env] environment started',
169
+ '[testing-env/test_feature@session1] feature setup session',
170
+ '[testing-env/test_feature@session1] feature teardown session with ValueError',
171
+ '[testing-env/test_feature] feature teardown',
172
+ '[testing-env] environment shutdown',
173
+ # pylint: enable=line-too-long
174
+ ]
175
+ )
176
+
177
+ def test_feature_housekeeping(self):
178
+ event_handler = TestingEventHandler(
179
+ log_sandbox_status=False,
180
+ log_feature_setup=False,
181
+ log_housekeep=True
182
+ )
183
+ env = TestingEnvironment(
184
+ image_ids=[],
185
+ features={
186
+ 'test_feature': TestingNonSandboxBasedFeature(
187
+ housekeep_interval=0.1
188
+ )
189
+ },
190
+ event_handler=event_handler,
191
+ housekeep_interval=0.2
192
+ )
193
+ with env:
194
+ env.wait_for_housekeeping()
195
+ self.assertIn(
196
+ '[testing-env/test_feature] feature housekeeping 0',
197
+ event_handler.logs
198
+ )
199
+
200
+ def test_feature_housekeeping_error(self):
201
+ event_handler = TestingEventHandler(
202
+ log_sandbox_status=False,
203
+ log_feature_setup=False,
204
+ log_housekeep=True
205
+ )
206
+ env = TestingEnvironment(
207
+ image_ids=[],
208
+ features={
209
+ 'test_feature': TestingNonSandboxBasedFeature(
210
+ simulate_housekeep_error=ValueError,
211
+ housekeep_interval=0.1
212
+ )
213
+ },
214
+ event_handler=event_handler,
215
+ housekeep_interval=0.2
216
+ )
217
+ with env:
218
+ env.wait_for_housekeeping()
219
+ self.assertFalse(env.is_online)
220
+ self.assertIn(
221
+ '[testing-env/test_feature] feature housekeeping 0 with ValueError',
222
+ event_handler.logs
223
+ )
224
+
225
+
226
+ if __name__ == '__main__':
227
+ unittest.main()
228
+