bitfab-py 0.76.4__tar.gz → 0.76.6__tar.gz

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.
Files changed (70) hide show
  1. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/PKG-INFO +1 -1
  2. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/client.py +99 -4
  3. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/provider_calls.py +88 -4
  4. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/pyproject.toml +2 -1
  5. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/README.md +0 -0
  6. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/__init__.py +0 -0
  7. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/agent_middleware.py +0 -0
  8. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/approval.py +0 -0
  9. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/assertion_categories.py +0 -0
  10. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/baml.py +0 -0
  11. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/baml_auto_trace.py +0 -0
  12. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/categories.py +0 -0
  13. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/claude_agent_sdk.py +0 -0
  14. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/client_patching.py +0 -0
  15. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/cloudReplay.py +0 -0
  16. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/cloud_replay_cli.py +0 -0
  17. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/commit_ref.py +0 -0
  18. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/compress.py +0 -0
  19. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/constants.py +0 -0
  20. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/datasets.py +0 -0
  21. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/db_snapshot.py +0 -0
  22. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/error_chain.py +0 -0
  23. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/experiment_id.py +0 -0
  24. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/experiments.py +0 -0
  25. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/finalizers.py +0 -0
  26. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/git_command.py +0 -0
  27. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/git_state.py +0 -0
  28. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/graders.py +0 -0
  29. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/http.py +0 -0
  30. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/integration_fixture.py +0 -0
  31. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/jev.py +0 -0
  32. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/labels.py +0 -0
  33. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/langgraph.py +0 -0
  34. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/langgraph_integration.py +0 -0
  35. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/langgraph_runs.py +0 -0
  36. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/mock_matching.py +0 -0
  37. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/mock_override.py +0 -0
  38. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/openai_agent_sdk.py +0 -0
  39. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/organization_members.py +0 -0
  40. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/otel.py +0 -0
  41. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/payload_budget.py +0 -0
  42. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/processor_payload.py +0 -0
  43. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/replay.py +0 -0
  44. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/replay_branch.py +0 -0
  45. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/replay_cli.py +0 -0
  46. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/replay_heartbeat.py +0 -0
  47. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/replay_interrupt.py +0 -0
  48. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/replay_invocation.py +0 -0
  49. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/replay_memory.py +0 -0
  50. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/replay_processes.py +0 -0
  51. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/replay_registry.py +0 -0
  52. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/seed_cli.py +0 -0
  53. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/selective_replay.py +0 -0
  54. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/serialize.py +0 -0
  55. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/simulation_plan.py +0 -0
  56. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/span_meta.py +0 -0
  57. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/span_origin.py +0 -0
  58. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/spans.py +0 -0
  59. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/subtree.py +0 -0
  60. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/thread_propagation.py +0 -0
  61. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/timestamp.py +0 -0
  62. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/trace_completion.py +0 -0
  63. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/trace_metadata.py +0 -0
  64. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/trace_payload.py +0 -0
  65. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/traces.py +0 -0
  66. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/tracing.py +0 -0
  67. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/transport.py +0 -0
  68. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/transport_types.py +0 -0
  69. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/verdicts.py +0 -0
  70. {bitfab_py-0.76.4 → bitfab_py-0.76.6}/bitfab/warn_once.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bitfab-py
3
- Version: 0.76.4
3
+ Version: 0.76.6
4
4
  Summary: Bitfab client for provider-based API calls with local BAML execution
5
5
  Author: Harvest Team
6
6
  Requires-Python: >=3.10
@@ -303,8 +303,9 @@ def _build_provider_auto_run(
303
303
  started_at = time.monotonic()
304
304
  with _hidden_original(original_code):
305
305
  response = await original(self, *args, **kwargs)
306
+ body = await provider_calls.parsed_body_async(response)
306
307
  provider_calls.record_auto_traced_completion(
307
- call, span, kwargs, response, _elapsed_ms(started_at)
308
+ call, span, kwargs, body, _elapsed_ms(started_at)
308
309
  )
309
310
  return response
310
311
 
@@ -1355,6 +1356,8 @@ class Bitfab:
1355
1356
  )
1356
1357
  if resolved_trace_across_threads:
1357
1358
  install_thread_propagation()
1359
+ provider_calls.install_import_hook()
1360
+ provider_calls.instrument_loaded_clients()
1358
1361
  start_commit_ref_resolution()
1359
1362
  self._simulation_plan.refresh()
1360
1363
 
@@ -5177,6 +5180,62 @@ class Bitfab:
5177
5180
  method="wrap_gemini",
5178
5181
  )
5179
5182
 
5183
+ def wrap_gemini_embeddings(
5184
+ self,
5185
+ fn: Callable[..., Any],
5186
+ *,
5187
+ on_response: Callable[[Any], None] | None = None,
5188
+ ) -> Callable[..., Any]:
5189
+ """Wrap a function that sends an ``embed_content`` request through the
5190
+ ``google-genai`` client to record the call's input, model, token
5191
+ usage, and duration. Bitfab works out the cost from the model and
5192
+ token counts.
5193
+
5194
+ The wrapper opens no span of its own. Each call writes onto the span
5195
+ that is current when the call starts, so call it inside a ``span``,
5196
+ ``trace``, or ``node``. Outside any span it records nothing and
5197
+ returns the response unchanged. The request's ``contents`` become the
5198
+ span's prompt, and one context entry holds ``provider``
5199
+ (``"gemini"``), ``model``, ``inputTokens`` (the sum of each
5200
+ embedding's ``statistics.token_count``), ``outputTokens`` (always 0),
5201
+ and ``durationMs``. Only Vertex AI returns ``statistics``, so a call
5202
+ to the Gemini Developer API records no token counts and no cost.
5203
+
5204
+ Beneath a ``trace`` root, ``models.embed_content`` calls on a
5205
+ ``google-genai`` client are already recorded without this wrapper as
5206
+ their own ``llm`` span named ``Models.embed_content``
5207
+ (``AsyncModels`` for ``client.aio``). Wrapping such a call records
5208
+ nothing more, and ``node`` on the wrapper configures that one span.
5209
+
5210
+ Example::
5211
+
5212
+ genai_client = genai.Client(
5213
+ vertexai=True, project=project, location=location
5214
+ )
5215
+ embed = client.wrap_gemini_embeddings(genai_client.models.embed_content)
5216
+ response = embed(model="text-embedding-005", contents=[text])
5217
+
5218
+ Args:
5219
+ fn: A function, sync or async, that sends the request and returns
5220
+ the embedding response.
5221
+ on_response: Called with each response after it is recorded.
5222
+ Errors it raises are ignored.
5223
+
5224
+ Returns:
5225
+ A wrapper with the same signature and name as ``fn``. A failed
5226
+ call raises its error unchanged and records nothing.
5227
+
5228
+ Raises:
5229
+ ValueError: If ``fn`` has no ``__name__``.
5230
+ """
5231
+ return self._wrap_provider_call(
5232
+ (provider_calls.GEMINI_EMBEDDINGS,),
5233
+ fn,
5234
+ on_response=on_response,
5235
+ example="genai_client.models.embed_content",
5236
+ method="wrap_gemini_embeddings",
5237
+ )
5238
+
5180
5239
  def _wrap_provider_call(
5181
5240
  self,
5182
5241
  calls: tuple[provider_calls.ProviderCall, ...],
@@ -5198,10 +5257,15 @@ class Bitfab:
5198
5257
  response: Any,
5199
5258
  started_at: float,
5200
5259
  framework_owned: bool,
5260
+ body: Any = None,
5201
5261
  ) -> Any:
5202
5262
  if not framework_owned and not call.recorded():
5203
5263
  provider_calls.record_completion(
5204
- call_type, span, request, response, _elapsed_ms(started_at)
5264
+ call_type,
5265
+ span,
5266
+ request,
5267
+ response if body is None else body,
5268
+ _elapsed_ms(started_at),
5205
5269
  )
5206
5270
  if on_response is not None:
5207
5271
  with contextlib.suppress(Exception):
@@ -5217,7 +5281,10 @@ class Bitfab:
5217
5281
  ) -> Any:
5218
5282
  with provider_calls.wrapper_call() as call:
5219
5283
  response = await awaitable
5220
- return finish(call, span, request, response, started_at, framework_owned)
5284
+ body = await provider_calls.parsed_body_async(response)
5285
+ return finish(
5286
+ call, span, request, response, started_at, framework_owned, body
5287
+ )
5221
5288
 
5222
5289
  if inspect.iscoroutinefunction(fn):
5223
5290
 
@@ -5228,8 +5295,9 @@ class Bitfab:
5228
5295
  with provider_calls.wrapper_call() as call:
5229
5296
  response = await fn(*args, **kwargs)
5230
5297
  request = provider_calls.request_of(args, kwargs)
5298
+ body = await provider_calls.parsed_body_async(response)
5231
5299
  return finish(
5232
- call, span, request, response, started_at, framework_owned
5300
+ call, span, request, response, started_at, framework_owned, body
5233
5301
  )
5234
5302
 
5235
5303
  wrapper: Callable[..., Any] = async_wrapper
@@ -6335,3 +6403,30 @@ class BitfabFunction:
6335
6403
  A wrapper with the same signature and name as ``fn``
6336
6404
  """
6337
6405
  return self._client.wrap_gemini(fn, on_response=on_response)
6406
+
6407
+ def wrap_gemini_embeddings(
6408
+ self,
6409
+ fn: Callable[..., Any],
6410
+ *,
6411
+ on_response: Callable[[Any], None] | None = None,
6412
+ ) -> Callable[..., Any]:
6413
+ """Wrap a function that sends an ``embed_content`` request through the
6414
+ ``google-genai`` client to record the call's input, model, token
6415
+ usage, and duration.
6416
+
6417
+ Delegates to ``Bitfab.wrap_gemini_embeddings``. See that method for
6418
+ details.
6419
+
6420
+ Like ``wrap_openrouter``, it does not use the bound key and opens no
6421
+ span of its own: it writes onto the span that is current when the call
6422
+ starts.
6423
+
6424
+ Args:
6425
+ fn: A function, sync or async, that sends the request and returns
6426
+ the embedding response
6427
+ on_response: Called with each response after it is recorded
6428
+
6429
+ Returns:
6430
+ A wrapper with the same signature and name as ``fn``
6431
+ """
6432
+ return self._client.wrap_gemini_embeddings(fn, on_response=on_response)
@@ -30,6 +30,7 @@ PARSE = "parse"
30
30
  STREAM = "stream"
31
31
  GENERATE_CONTENT = "generate_content"
32
32
  GENERATE_CONTENT_STREAM = "generate_content_stream"
33
+ EMBED_CONTENT = "embed_content"
33
34
  PATCHED_MARKER = "_bitfab_provider_call_patched"
34
35
  CALL_MARKER = "_bitfab_provider_call"
35
36
  OPENROUTER_HOSTNAME = "openrouter.ai"
@@ -518,6 +519,36 @@ def _gemini_usage(request: Mapping[str, Any], response: Any) -> dict[str, Any]:
518
519
  )
519
520
 
520
521
 
522
+ def _is_gemini_embedding_response(response: Any) -> bool:
523
+ return isinstance(_field(response, "embeddings"), (list, tuple))
524
+
525
+
526
+ def _gemini_embedding_prompt(request: Mapping[str, Any]) -> str | None:
527
+ return _encoded(_gemini_turns(request.get("contents")))
528
+
529
+
530
+ def _gemini_embedding_token_count(embedding: Any) -> int | float | None:
531
+ statistics = _field(embedding, "statistics")
532
+ count = _number(_field(statistics, "token_count"))
533
+ return count if count is not None else _number(_field(statistics, "tokenCount"))
534
+
535
+
536
+ def _gemini_embedding_usage(
537
+ request: Mapping[str, Any], response: Any
538
+ ) -> dict[str, Any]:
539
+ embeddings = _field(response, "embeddings") or []
540
+ input_tokens = _sum_tokens(
541
+ *(_gemini_embedding_token_count(embedding) for embedding in embeddings)
542
+ )
543
+ return _present(
544
+ {
545
+ "model": _gemini_model(request, response),
546
+ "inputTokens": input_tokens,
547
+ "outputTokens": None if input_tokens is None else 0,
548
+ }
549
+ )
550
+
551
+
521
552
  def _gemini_chunk_text(chunk: Any) -> str:
522
553
  candidates = _field(chunk, "candidates")
523
554
  if not isinstance(candidates, (list, tuple)) or not candidates:
@@ -739,6 +770,15 @@ def _observe(call: ProviderCall, stream: Any, recording: StreamRecording) -> Any
739
770
  return observed
740
771
 
741
772
 
773
+ def _observe_returned(
774
+ call: ProviderCall, returned: Any, body: Any, recording: StreamRecording
775
+ ) -> Any:
776
+ if body is returned:
777
+ return _observe(call, returned, recording)
778
+ _observe(call, body, recording)
779
+ return returned
780
+
781
+
742
782
  def _finish_when_dropped(stream: Any, recording: StreamRecording) -> None:
743
783
  with contextlib.suppress(TypeError):
744
784
  weakref.finalize(stream, recording.finish, None).atexit = False
@@ -755,22 +795,24 @@ def _streamed_call(
755
795
  async def async_run(resource: Any, *args: Any, **kwargs: Any) -> Any:
756
796
  recording = _start_recording(call, span_name, kwargs, node_owned)
757
797
  try:
758
- stream = await original(resource, *args, **kwargs)
798
+ returned = await original(resource, *args, **kwargs)
759
799
  except BaseException as error:
760
800
  recording.finish(error)
761
801
  raise
762
- return _observe(call, stream, recording)
802
+ body = await parsed_body_async(returned, streamed=True)
803
+ return _observe_returned(call, returned, body, recording)
763
804
 
764
805
  return async_run
765
806
 
766
807
  def run(resource: Any, *args: Any, **kwargs: Any) -> Any:
767
808
  recording = _start_recording(call, span_name, kwargs, node_owned)
768
809
  try:
769
- stream = original(resource, *args, **kwargs)
810
+ returned = original(resource, *args, **kwargs)
770
811
  except BaseException as error:
771
812
  recording.finish(error)
772
813
  raise
773
- return _observe(call, stream, recording)
814
+ body = parsed_body(returned, streamed=True)
815
+ return _observe_returned(call, returned, body, recording)
774
816
 
775
817
  return run
776
818
 
@@ -847,6 +889,15 @@ GEMINI = ProviderCall(
847
889
  stream_summary=_gemini_stream_summary,
848
890
  )
849
891
 
892
+ GEMINI_EMBEDDINGS = dataclasses.replace(
893
+ GEMINI,
894
+ is_response=_is_gemini_embedding_response,
895
+ prompt=_gemini_embedding_prompt,
896
+ usage=_gemini_embedding_usage,
897
+ method=EMBED_CONTENT,
898
+ stream_summary=None,
899
+ )
900
+
850
901
  PROVIDER_CALLS = (
851
902
  OPENROUTER,
852
903
  OPENAI_CHAT,
@@ -867,6 +918,7 @@ PROVIDER_CALLS = (
867
918
  dataclasses.replace(OPENAI_RESPONSES, method=PARSE),
868
919
  GEMINI,
869
920
  dataclasses.replace(GEMINI, method=GENERATE_CONTENT_STREAM, always_streams=True),
921
+ GEMINI_EMBEDDINGS,
870
922
  )
871
923
  _BY_MODULE: dict[str, tuple[ProviderCall, ...]] = {}
872
924
  for _call in PROVIDER_CALLS:
@@ -890,6 +942,37 @@ def request_of(args: tuple[Any, ...], kwargs: Mapping[str, Any]) -> Mapping[str,
890
942
  return args[0]
891
943
 
892
944
 
945
+ def _raw_response_parse(response: Any, streamed: bool) -> Callable[[], Any] | None:
946
+ parse = getattr(response, "parse", None)
947
+ http_response = getattr(response, "http_response", None)
948
+ if not callable(parse) or http_response is None:
949
+ return None
950
+ if streamed or getattr(http_response, "is_closed", False) is True:
951
+ return parse
952
+ return None
953
+
954
+
955
+ def parsed_body(response: Any, *, streamed: bool = False) -> Any:
956
+ parse = _raw_response_parse(response, streamed)
957
+ if parse is None or is_async_create(parse):
958
+ return response
959
+ try:
960
+ return parse()
961
+ except Exception:
962
+ return response
963
+
964
+
965
+ async def parsed_body_async(response: Any, *, streamed: bool = False) -> Any:
966
+ parse = _raw_response_parse(response, streamed)
967
+ if parse is None:
968
+ return response
969
+ try:
970
+ body = parse()
971
+ return await body if inspect.isawaitable(body) else body
972
+ except Exception:
973
+ return response
974
+
975
+
893
976
  def record_completion(
894
977
  call: ProviderCall,
895
978
  span: Any,
@@ -897,6 +980,7 @@ def record_completion(
897
980
  response: Any,
898
981
  duration_ms: int,
899
982
  ) -> None:
983
+ response = parsed_body(response)
900
984
  if not call.is_response(response):
901
985
  return
902
986
  with contextlib.suppress(Exception):
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "bitfab-py"
3
- version = "0.76.4"
3
+ version = "0.76.6"
4
4
  description = "Bitfab client for provider-based API calls with local BAML execution"
5
5
  authors = ["Harvest Team"]
6
6
  readme = "README.md"
@@ -49,6 +49,7 @@ bump-my-version = "^1.6.3"
49
49
  typer = "^0.27.3"
50
50
  ruff = ">=0.17.2,<1.0.0"
51
51
  deepagents = {version = ">=0.7.30", python = ">=3.11,<4.0"}
52
+ braintrust = ">=0.49.2,<1.0.0"
52
53
 
53
54
  [build-system]
54
55
  requires = ["poetry-core"]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes