Skip to content

Commit c94f595

Browse files
authored
examples: fix broken examples and unblock their type checks (#594)
* examples: fix broken examples and unblock their type checks - chat_with_image: dead image URL returned 400; use a live image - structured_outputs: mistral-large-2411 rejected as invalid model; use mistral-large-latest - run_example_scripts.yaml: install the [telemetry] extra so observability examples run - redaction.py: import SpanExporter as the base class so mypy accepts it (valid-type) - pre-commit: add pydantic to the pyright hook deps - guard optional message access in the two examples * examples: use a stable pinned image URL Replace the mistral.ai _astro build asset (per-deploy content hash, would 404 on redeploy) with the Mistral logo pinned to a commit SHA so it can never rot. Addresses review feedback.
1 parent 7a87303 commit c94f595

5 files changed

Lines changed: 15 additions & 7 deletions

File tree

‎.github/workflows/run_example_scripts.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
3535
- name: Install client with extras
3636
run: |
37-
PACKAGE="dist/$(ls dist | grep whl | head -n 1)[agents]"
37+
PACKAGE="dist/$(ls dist | grep whl | head -n 1)[agents,telemetry]"
3838
uv pip install --system "$PACKAGE"
3939
4040
examples:
@@ -73,7 +73,7 @@ jobs:
7373
7474
- name: Install client with extras and run all examples.
7575
run: |
76-
PACKAGE="dist/$(ls dist | grep whl | head -n 1)[agents]"
76+
PACKAGE="dist/$(ls dist | grep whl | head -n 1)[agents,telemetry]"
7777
uv pip install --system "$PACKAGE"
7878
./scripts/run_examples.sh
7979
env:

‎.pre-commit-config.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ repos:
1010
rev: v1.1.401
1111
hooks:
1212
- id: pyright
13+
additional_dependencies: [pydantic]
1314
files: ^(examples/|src/mistralai/|packages/(azure|gcp)/src/mistralai/).*\.py$
1415
exclude: ^src/mistralai/(__init__|sdkhooks|types)\.py$
1516
- repo: https://github.com/pre-commit/mirrors-mypy

‎examples/mistral/chat/async_chat_with_image_no_streaming.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ async def main():
2121
{"type": "text", "text": "What's in this image?"},
2222
{
2323
"type": "image_url",
24-
"image_url": "https://cms.mistral.ai/assets/a64b3821-3a4c-4d4d-b718-d653f3eb7a5e.png?",
24+
"image_url": "https://raw.githubusercontent.com/mistralai/mistral-common/7edf6f651b3579135f44686e345d51b7e19a536a/docs/assets/logo_favicon.png",
2525
},
2626
]
2727
)
2828
],
2929
)
3030

31-
print(chat_response.choices[0].message.content)
31+
if chat_response.choices:
32+
message = chat_response.choices[0].message
33+
if message is not None:
34+
print(message.content)
3235

3336

3437
if __name__ == "__main__":

‎examples/mistral/chat/async_structured_outputs.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class MathDemonstration(BaseModel):
2020
final_answer: str
2121

2222
chat_response = await client.chat.parse_async(
23-
model="mistral-large-2411",
23+
model="mistral-large-latest",
2424
messages=[
2525
{
2626
"role": "system",
@@ -30,7 +30,10 @@ class MathDemonstration(BaseModel):
3030
],
3131
response_format=MathDemonstration,
3232
)
33-
print(chat_response.choices[0].message.parsed)
33+
if chat_response.choices:
34+
message = chat_response.choices[0].message
35+
if message is not None:
36+
print(message.parsed)
3437

3538

3639
if __name__ == "__main__":

‎src/mistralai/extra/observability/redaction.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
# Inherit from the real base only for static analysis: linters verify our
3030
# export/shutdown/force_flush signatures. At runtime the base is object so
3131
# the optional OpenTelemetry SDK is not required to import this module.
32-
_SpanExporterBase = SpanExporter
32+
# Imported (not assigned to a variable) so type checkers accept it as a base.
33+
from opentelemetry.sdk.trace.export import SpanExporter as _SpanExporterBase
3334
else:
3435
_SpanExporterBase = object
3536

0 commit comments

Comments
 (0)